示例#1
0
 public T GetResult()
 {
     Debug.Assert(IsCompleted);
     if (!_taskAwaiter.IsCompleted)
     {
         _cancellationToken.ThrowIfCancellationRequested();
     }
     return(_taskAwaiter.GetResult());
 }
示例#2
0
            //****************************************

            private void OnContinuePeekDecrement()
            {
                try
                {
                    if (_Awaiter.GetResult())
                    {
                        _Operation !.OnPeekCompleted(_Collection !);
                    }
                    else
                    {
                        _Operation !.OnPeekFailed();
                    }
                }
                catch
                {
                    _Operation !.OnPeekFailed();
                }
                finally
                {
                    Release();
                }
            }
示例#3
0
 TResult ITaskAwaiter <TResult> .GetResult() => _cfgAwaiter.GetResult();
            public void MoveNext()
            {
                try
                {
                    bool waitToReadResult = false;
                    switch ((State)_state)
                    {
                    case State.OuterLoop:
                        ValueTask <bool> waitToReadTask = _reader.WaitToReadAsync(_cancellationToken);
                        if (waitToReadTask.IsCompleted)
                        {
                            // WaitToReadAsync completed.  Get the result and jump to process it.
                            waitToReadResult = waitToReadTask.GetAwaiter().GetResult();
                            _state           = (int)State.HaveWaitToReadResult;
                            goto case State.HaveWaitToReadResult;
                        }

                        // WaitToReadAsync wasn't yet complete.  Mark that the promise
                        // is being used for the result, set where we should return to when it completes,
                        // store the awaiter, and hook up the continuation.
                        _usePromiseForResult = true;
                        _state             = (int)State.FinishingAwait;
                        _waitToReadAwaiter = waitToReadTask.ConfigureAwait(false).GetAwaiter();
                        AsyncEnumerable inst = this;
                        _builder.AwaitUnsafeOnCompleted(ref _waitToReadAwaiter, ref inst);
                        return;

                    case State.FinishingAwait:
                        // The await on WaitToReadAsync finished.  Get its result and process it.
                        waitToReadResult = _waitToReadAwaiter.GetResult();
                        _state           = (int)State.HaveWaitToReadResult;
                        goto case State.HaveWaitToReadResult;

                    case State.HaveWaitToReadResult:
                        // We have a result from WaitToReadAsync.  If an item might be available,
                        // jump to try to read it.  If an item will never be available, complete.
                        if (waitToReadResult)
                        {
                            _state = (int)State.TryRead;
                            goto case State.TryRead;
                        }
                        else
                        {
                            _state = (int)State.Completing;
                            goto case State.Completing;
                        }

                    case State.TryRead:
                        // Do the read.  If we successfully get an item, mark that an item is
                        // available.  Then if we've already awaited as part of this MoveNextAsync,
                        // also complete the promise.
                        if (_reader.TryRead(out _current))
                        {
                            _itemAvailable = true;
                            if (_usePromiseForResult)
                            {
                                _promise.SetResult(true);
                            }
                            return;
                        }
                        else
                        {
                            // No item was available.  Start over.
                            _state = (int)State.OuterLoop;
                            goto case State.OuterLoop;
                        }

                    case State.Completing:
                        // Cleanup.  And if there's an outstanding promise, complete it
                        // to indicate iteration is done.
                        _builder.Complete();
                        _state = (int)State.Done;
                        if (_usePromiseForResult)
                        {
                            _promise.SetResult(false);
                        }
                        return;
                    }
                }
                catch (Exception e)
                {
                    _state = (int)State.Done;
                    _builder.Complete();
                    _itemAvailable       = false;
                    _usePromiseForResult = true;
                    _promise.SetException(e);
                }
            }