Пример #1
0
        private async Task <int> ConsumerWithWait <TItem>(AsyncCollection <TItem> collection, TimeSpan delay)
        {
            var TotalItems = 0;

            try
            {
                await foreach (var MyResult in collection.GetConsumingAsyncEnumerable())
                {
                    TotalItems++;

                    if (delay == TimeSpan.Zero)
                    {
                        await Task.Yield();
                    }
                    else
                    {
                        await Task.Delay(delay);
                    }
                }
            }
            catch (InvalidOperationException)
            {
                // Cancelled
            }

            return(TotalItems);
        }
Пример #2
0
        private async Task <int> Consumer <TItem>(AsyncCollection <TItem> collection)
        {
            var TotalItems = 0;

            try
            {
                await foreach (var MyResult in collection.GetConsumingAsyncEnumerable())
                {
                    TotalItems++;
                }
            }
            catch (InvalidOperationException)
            {
                // Cancelled
            }

            return(TotalItems);
        }