/// <summary>
        /// Clears all pending requests, and waits for any active request to complete
        /// </summary>
        public void Dispose()
        {
            IAsyncResult resultReply = null;
            KeyValuePair <WaitCallback, object>?originalRequest = null;

            lock (_syncLock)
            {
                if (_requests != null)
                {
                    if (_requests.Count > 0)
                    {
                        originalRequest = _requests.Dequeue();
                    }
                    _requests.Clear();
                    _requests = null;
                }
                resultReply         = _pendingReplyResult;
                _pendingReplyResult = null;
            }

            if (originalRequest != null && resultReply == null)
            {
                // We need to wait for the request to complete
                while (originalRequest != null)
                {
                    System.Threading.Thread.Sleep(10);
                    lock (_syncLock)
                    {
                        if (_pendingReplyResult != null)
                        {
                            originalRequest     = null;
                            resultReply         = _pendingReplyResult;
                            _pendingReplyResult = null;
                        }
                    }
                }
            }

            if (resultReply != null)
            {
                try
                {
                    WaitCallback action = resultReply.AsyncState as WaitCallback;
                    action.EndInvoke(resultReply);
                }
                catch
                {
                }
            }
        }
        /// <summary>
        /// Checks the result of the last request, and starts execution of the next pending request
        /// </summary>
        public object CheckResult()
        {
            KeyValuePair <WaitCallback, object>?nextPendingRequest = null;
            KeyValuePair <WaitCallback, object>?originalRequest    = null;
            IAsyncResult resultReply = null;

            lock (_syncLock)
            {
                if (_requests == null)
                {
                    return(null);    // Has been disposed
                }
                if (_pendingReplyResult != null)
                {
                    resultReply     = _pendingReplyResult;
                    originalRequest = _requests.Dequeue();
                    if (_requests.Count > 0)
                    {
                        nextPendingRequest = _requests.Peek();
                    }
                    _pendingReplyResult = null;
                }
            }

            if (nextPendingRequest.HasValue)
            {
                nextPendingRequest.Value.Key.BeginInvoke(nextPendingRequest.Value.Value, RequestCallback, nextPendingRequest.Value.Key);
            }

            if (resultReply != null)
            {
                WaitCallback action = resultReply.AsyncState as WaitCallback;
                action.EndInvoke(resultReply);  // Can throw if any pending exception
            }

            if (originalRequest.HasValue)
            {
                return(originalRequest.Value.Value);
            }
            else
            {
                return(null);
            }
        }
Exemple #3
0
 /// <summary>
 ///     Implements <see cref="IBackgroundDispatcher.EndInvoke(IAsyncResult)" />.
 /// </summary>
 public void EndInvoke(IAsyncResult asyncResult)
 {
     _invokerWaitCallback.EndInvoke(asyncResult);
 }