Пример #1
0
        /// <summary>
        /// Advances the enumerator to the next element of the collection asynchronously
        /// </summary>
        /// <param name="cancellationToken">A cancellation token to cancel the enumeration</param>
        /// <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 Task <bool> MoveNextAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            if (_enumerationFunction == null)
            {
                return(TaskEx.False);
            }

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

            var moveNextCompleteTask = _yield.OnMoveNext(cancellationToken);

            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);
        }
            private static async Task _enumerate(AsyncEnumerator <TSource> .Yield yield, SkipWhileContext <TSource> context)
            {
                try
                {
                    var yielding = false;
                    while (await context.Source.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
                    {
                        if (!yielding && !context.Predicate(context.Source.Current))
                        {
                            yielding = true;
                        }

                        if (yielding)
                        {
                            await yield.ReturnAsync(context.Source.Current).ConfigureAwait(false);
                        }
                    }
                }
                finally
                {
                    if (context.DisposeSource)
                    {
                        context.Source.Dispose();
                    }
                }
            }
 private static async Task _enumerate(AsyncEnumerator <TSource> .Yield yield, SkipContext <TSource> context)
 {
     try
     {
         var itemsToSkip = context.Count;
         while (await context.Source.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
         {
             if (itemsToSkip > 0)
             {
                 itemsToSkip--;
             }
             else
             {
                 await yield.ReturnAsync(context.Source.Current).ConfigureAwait(false);
             }
         }
     }
     finally
     {
         if (context.DisposeSource)
         {
             context.Source.Dispose();
         }
     }
 }
 private static async Task _enumerate(AsyncEnumerator <TResult> .Yield yield, SelectContext <TSource, TResult> context)
 {
     using (var enumerator = await context.Source.GetAsyncEnumeratorAsync(yield.CancellationToken).ConfigureAwait(false))
     {
         while (await enumerator.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
         {
             await yield.ReturnAsync(context.Selector(enumerator.Current)).ConfigureAwait(false);
         }
     }
 }
 private static async Task _enumerate(AsyncEnumerator <TSource> .Yield yield, WhereContext <TSource> context)
 {
     using (var enumerator = await context.Source.GetAsyncEnumeratorAsync(yield.CancellationToken).ConfigureAwait(false))
     {
         while (await enumerator.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
         {
             if (context.Predicate(enumerator.Current))
             {
                 await yield.ReturnAsync(enumerator.Current).ConfigureAwait(false);
             }
         }
     }
 }
 private static async Task _enumerate(AsyncEnumerator <TSource> .Yield yield, TakeContext <TSource> context)
 {
     using (var enumerator = await context.Source.GetAsyncEnumeratorAsync(yield.CancellationToken).ConfigureAwait(false))
     {
         for (var i = context.Count; i > 0; i--)
         {
             if (await enumerator.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
             {
                 await yield.ReturnAsync(enumerator.Current).ConfigureAwait(false);
             }
         }
     }
 }
 private static async Task _enumerate(AsyncEnumerator <TResult> .Yield yield, SelectContext <TSource, TResult> context)
 {
     try
     {
         while (await context.Source.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
         {
             await yield.ReturnAsync(context.Selector(context.Source.Current)).ConfigureAwait(false);
         }
     }
     finally
     {
         if (context.DisposeSource)
         {
             context.Source.Dispose();
         }
     }
 }
 private static async Task _enumerate(AsyncEnumerator <TSource> .Yield yield, SkipContext <TSource> context)
 {
     using (var enumerator = await context.Source.GetAsyncEnumeratorAsync(yield.CancellationToken).ConfigureAwait(false))
     {
         var itemsToSkip = context.Count;
         while (await enumerator.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
         {
             if (itemsToSkip > 0)
             {
                 itemsToSkip--;
             }
             else
             {
                 await yield.ReturnAsync(enumerator.Current).ConfigureAwait(false);
             }
         }
     }
 }
Пример #9
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;
        }
            private static async Task _enumerate(AsyncEnumerator <TSource> .Yield yield, SkipWhileContext <TSource> context)
            {
                using (var enumerator = await context.Source.GetAsyncEnumeratorAsync(yield.CancellationToken).ConfigureAwait(false))
                {
                    var yielding = false;
                    while (await enumerator.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
                    {
                        if (!yielding && !context.Predicate(enumerator.Current))
                        {
                            yielding = true;
                        }

                        if (yielding)
                        {
                            await yield.ReturnAsync(enumerator.Current).ConfigureAwait(false);
                        }
                    }
                }
            }
 private static async Task _enumerate(AsyncEnumerator <TSource> .Yield yield, TakeContext <TSource> context)
 {
     try
     {
         for (var i = context.Count; i > 0; i--)
         {
             if (await context.Source.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
             {
                 await yield.ReturnAsync(context.Source.Current).ConfigureAwait(false);
             }
         }
     }
     finally
     {
         if (context.DisposeSource)
         {
             context.Source.Dispose();
         }
     }
 }
 private static async Task _enumerate(AsyncEnumerator <TSource> .Yield yield, WhereWithIndexContext <TSource> context)
 {
     try
     {
         long index = 0;
         while (await context.Source.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
         {
             if (context.Predicate(context.Source.Current, index))
             {
                 await yield.ReturnAsync(context.Source.Current).ConfigureAwait(false);
             }
             index++;
         }
     }
     finally
     {
         if (context.DisposeSource)
         {
             context.Source.Dispose();
         }
     }
 }
 private static async Task _enumerate(AsyncEnumerator <TSource> .Yield yield, TakeWhileContext <TSource> context)
 {
     try
     {
         while (await context.Source.MoveNextAsync(yield.CancellationToken).ConfigureAwait(false))
         {
             if (context.Predicate(context.Source.Current))
             {
                 await yield.ReturnAsync(context.Source.Current).ConfigureAwait(false);
             }
             else
             {
                 break;
             }
         }
     }
     finally
     {
         if (context.DisposeSource)
         {
             context.Source.Dispose();
         }
     }
 }
Пример #14
0
        public Task <IAsyncEnumerator <T> > GetAsyncEnumeratorAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var enumerator = new AsyncEnumerator <T>(_enumerationFunction, _oneTimeUse);

            return(Task.FromResult <IAsyncEnumerator <T> >(enumerator));
        }