/// <summary> /// Begins the connect. /// </summary> /// <param name="callback">The callback.</param> /// <param name="state">The state.</param> /// <returns>Async property</returns> public virtual IAsyncResult BeginConnect(AsyncCallback callback, object state) { Interlocked.Increment(ref mAsyncActiveConnectCount); ChannelConnectAgainDelegate d = new ChannelConnectAgainDelegate(this.Connect); if (this.mAsyncActiveConnectEvent == null) { lock (this) { if (this.mAsyncActiveConnectEvent == null) { this.mAsyncActiveConnectEvent = new AutoResetEvent(true); } } } this.mAsyncActiveConnectEvent.WaitOne(); this.mConnectAgainDelegate = d; return(d.BeginInvoke(callback, state)); }
/// <summary> /// Ends the connect. /// </summary> /// <param name="asyncResult">The async result.</param> /// <returns>SessionId</returns> public virtual string EndConnect(IAsyncResult asyncResult) { if (asyncResult == null) { ThrowHelper.ThrowArgumentNullException("asyncResult"); } if (this.mConnectAgainDelegate == null) { ThrowHelper.ThrowArgumentException("Wrong async result or EndConnect called multiple times.", "asyncResult"); } try { return(this.mConnectAgainDelegate.EndInvoke(asyncResult)); } finally { this.mConnectAgainDelegate = null; this.mAsyncActiveConnectEvent.Set(); CloseAsyncActiveConnectEvent(Interlocked.Decrement(ref mAsyncActiveConnectCount)); } }