Пример #1
0
        /// <summary>
        /// Advances the enumerator to the next element of the collection asynchronously
        /// </summary>
        /// <returns>Returns a Task that does transition to the next element. The result of the task is True if the enumerator was successfully advanced to the next element, or False if the enumerator has passed the end of the collection.</returns>
        public virtual ValueTask <bool> MoveNextAsync()
        {
            if (_enumerationFunction == null)
            {
                return(new ValueTask <bool>(false));
            }

            if (_yield == null)
            {
                _yield = new AsyncEnumerator <TItem> .Yield(this, MasterCancellationToken);
            }

            var moveNextCompleteTask = _yield.OnMoveNext();

            if (_enumerationTask == null)
            {
                // Register for finalization, which might be needed if caller
                // doesn't not finish the enumeration and does not call Dispose().
                GC.ReRegisterForFinalize(this);

                _enumerationTask =
                    _enumerationFunction(_yield, State)
                    .ContinueWith(OnEnumerationCompleteAction, this, TaskContinuationOptions.ExecuteSynchronously);
            }

            return(moveNextCompleteTask);
        }
Пример #2
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources
        /// </summary>
        /// <param name="manualDispose">True if called from Dispose() method, otherwise False - called by GC</param>
        protected virtual void Dispose(bool manualDispose)
        {
            if (manualDispose)
            {
                _yield?.SetCanceled();
            }
            else if (_yield != null && !_yield.IsComplete)
            {
                var yield = _yield;
                Task.Run(() => yield.SetCanceled()); // don't block the GC thread
            }

            _enumerationTask = null;
            _yield           = null;

            _onDisposeAction?.Invoke(State);
            _onDisposeAction     = null;
            _enumerationFunction = null;
        }