Пример #1
0
        public IAsyncResult BeginAsync(AsyncCallback callback, object state)
        {
            if (_waitHandle != null)
            {
                throw new InvalidOperationException("The asynchronous request was already started.");
            }

            _state = state;
            lock (_completionCallbacks)
                _completionCallbacks.Add(callback);

            WaitOrTimerCallback timerCallback = (s, timeoutExpired) =>
            {
                if (timeoutExpired)
                {
                    lock (_completionCallbacks)
                        _completionCallbacks.Add(asyncResult => _timeoutCallback());

                    Fail(RequestTimeoutException.FromCorrelationId(_requestId));
                }
            };

            _waitHandle = ThreadPool.RegisterWaitForSingleObject(CompleteEvent, timerCallback, state, _timeout, true);

            return(this);
        }
Пример #2
0
        public bool Wait()
        {
            bool alreadyCompleted;

            lock (_lock)
                alreadyCompleted = _completed;

            bool result = alreadyCompleted || CompleteEvent.WaitOne(_timeout == TimeSpan.MaxValue ? Int32.MaxValue : (int)_timeout.TotalMilliseconds, true);

            if (!result)
            {
                Fail(RequestTimeoutException.FromCorrelationId(_requestId));
            }

            Close();

            if (_exception != null)
            {
                throw _exception;
            }

            return(result);
        }