/// <summary> /// Start an asynchronous receive operation. /// </summary> public IAsyncResult BeginReceive(AsyncCallback callback, object state) { // Formatter check if (!IsOpen()) { throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen); } if (waitingMessages.Count == 0) // Can't start as no messages exist! { return(null); } // Setup wroker Worker w = new Worker(); // Create a new instance of the formatter w.Formatter = (IXmlStructureFormatter)Formatter; // Set async result IAsyncResult Result = new ReceiveResultAsyncResult(state, new AutoResetEvent(false)); // Completed delegate w.Completed += delegate(object Sender) { Worker sWorker = Sender as Worker; // Strong type sender // Lookup the result in the dictionary if (!asyncResults.ContainsKey(Result)) { lock (asyncResults) { asyncResults.Add(Result, sWorker.ReceiveResult); } } (Result as ReceiveResultAsyncResult).SetComplete(); // Set completed (Result.AsyncWaitHandle as AutoResetEvent).Set(); // send signal if (callback != null) { callback(Result); // callback } }; // Add to thread pool KeyValuePair <Message, Guid> mData; lock (waitingMessages) { mData = waitingMessages.Dequeue(); } w.MessageId = mData.Value; ThreadPool.QueueUserWorkItem(new WaitCallback(w.WorkReceive), mData.Key); return(Result); }
/// <summary> /// Begin an asynchronous receive event. /// </summary> /// <param name="correlate">The result of the send method.</param> /// <param name="callback">A callback.</param> /// <param name="state">User state.</param> public IAsyncResult BeginReceive(ISendResult correlate, AsyncCallback callback, object state) { // Formatter check if (!IsOpen()) { throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen); } // Create the work that will perform the operations Worker w = new Worker(); w.Formatter = (IXmlStructureFormatter)Formatter; #if WINDOWS_PHONE w.Actions = this.m_actions; #else w.WcfConfiguration = wcfConfiguration; #endif w.MessageVersion = wcfClient.Endpoint.Binding.MessageVersion; Message data = null; if (!results.TryGetValue(correlate, out data)) { return(null); } // The asynchronous result, used to correlate results when EndReceive is called IAsyncResult result = new ReceiveResultAsyncResult(state, new AutoResetEvent(false)); // Work w.Completed += new WaitCallback(delegate(object sender) { lock (asyncResults) asyncResults.Add(result, (sender as Worker).ReceiveResult); (result.AsyncWaitHandle as AutoResetEvent).Set(); }); ThreadPool.QueueUserWorkItem(w.WorkReceive, data); return(result); }
/// <summary> /// Start an asynchronous receive operation. /// </summary> public IAsyncResult BeginReceive(AsyncCallback callback, object state) { // Formatter check if (!IsOpen()) throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen); if (waitingMessages.Count == 0) // Can't start as no messages exist! return null; // Setup wroker Worker w = new Worker(); // Create a new instance of the formatter w.Formatter = (IXmlStructureFormatter)Formatter; // Set async result IAsyncResult Result = new ReceiveResultAsyncResult(state, new AutoResetEvent(false)); // Completed delegate w.Completed += delegate(object Sender) { Worker sWorker = Sender as Worker; // Strong type sender // Lookup the result in the dictionary if (!asyncResults.ContainsKey(Result)) lock (asyncResults) { asyncResults.Add(Result, sWorker.ReceiveResult); } (Result as ReceiveResultAsyncResult).SetComplete(); // Set completed (Result.AsyncWaitHandle as AutoResetEvent).Set(); // send signal if (callback != null) callback(Result); // callback }; // Add to thread pool KeyValuePair<Message, Guid> mData; lock (waitingMessages) { mData = waitingMessages.Dequeue(); } w.MessageId = mData.Value; ThreadPool.QueueUserWorkItem(new WaitCallback(w.WorkReceive), mData.Key); return Result; }
/// <summary> /// Begin an asynchronous receive event. /// </summary> /// <param name="correlate">The result of the send method.</param> /// <param name="callback">A callback.</param> /// <param name="state">User state.</param> public IAsyncResult BeginReceive(ISendResult correlate, AsyncCallback callback, object state) { // Formatter check if (!IsOpen()) throw new ConnectorException(ConnectorException.MSG_INVALID_STATE, ConnectorException.ReasonType.NotOpen); // Create the work that will perform the operations Worker w = new Worker(); w.Formatter = (IXmlStructureFormatter)Formatter; #if WINDOWS_PHONE w.Actions = this.m_actions; #else w.WcfConfiguration = wcfConfiguration; #endif w.MessageVersion = wcfClient.Endpoint.Binding.MessageVersion; Message data = null; if (!results.TryGetValue(correlate, out data)) return null; // The asynchronous result, used to correlate results when EndReceive is called IAsyncResult result = new ReceiveResultAsyncResult(state, new AutoResetEvent(false)); // Work w.Completed += new WaitCallback(delegate(object sender) { lock (asyncResults) asyncResults.Add(result, (sender as Worker).ReceiveResult); (result.AsyncWaitHandle as AutoResetEvent).Set(); }); ThreadPool.QueueUserWorkItem(w.WorkReceive, data); return result; }