void DisposeImpl() { var y = _yielder; if (y != null) { y.Break(); _yielder = null; } // Let the task finish on its own _task = null; }
public async Task <bool> MoveNext(CancellationToken cancellationToken) { if (_task != null) { // Second MoveNext() call. Tell Yielder to let the function continue. _yielder.CancellationToken = cancellationToken; _yielder.Continue(); } else { if (_func == null) { throw new AsyncYielderDisposedException(); } // First MoveNext() call. Start the task. _yielder = new AsyncYielder <T>(); _yielder.CancellationToken = cancellationToken; _task = _func(_yielder); _func = null; } // Wait for yield or return. Task finished = await Task.WhenAny(_task, _yielder.YieldTask).ConfigureAwait(false); var y = _yielder; if (finished != _task && y != null) { // the function returned a result. Current = y.YieldTask.Result; return(true); } // The operation is finished. Task t = _task; _yielder = null; _task = null; if (t != null && t.IsFaulted) { throw t.Exception; } return(false); }