private void ContinueEnumerator(bool outsideOfSyncContext) { if (_syncContext != null && outsideOfSyncContext) { _syncContext.Post(SyncContextContinueEnumerator, this); return; } Exception caughtException = null; lock (_enumeratorLock) { bool stillGoing = false; try { while ((stillGoing = _enumerator.MoveNext())) { if (HasBeenCancelled()) { continue; } int expectedResultCount = _enumerator.Current; if (expectedResultCount == 0) { ThreadPool.QueueUserWorkItem(ThreadPoolContinueEnumerator, this); return; } _waitCount.WriteLock(x => expectedResultCount); return; } } catch (Exception ex) { caughtException = ex; throw; } finally { if (!stillGoing) { _enumerator.Dispose(); if (caughtException != null) { _asyncResult.SetAsCompleted(caughtException); } else { _asyncResult.SetAsCompleted(); } } } } }
public IAsyncResult Result() { return(_resultQueue.WriteLock(x => x.Dequeue())); }