示例#1
0
 private async Task CallAsync(AsyncSemaphore semaphore, int i)
 {
     try
     {
         await this.doAsync();
     }
     finally
     {
         semaphore.Release();
     }
 }
示例#2
0
        public async Task RunAsync(int totalCalls)
        {
            AsyncSemaphore semaphore = new AsyncSemaphore(this.maxPendingCalls);
            Queue<Task> pending = new Queue<Task>();
            List<Exception> exceptions = new List<Exception>();
            for (int i = 0; i < totalCalls; ++i)
            {
                await semaphore.WaitAsync();
                pending.Enqueue(this.CallAsync(semaphore, i));
                HandleCompletedCalls(pending, exceptions);
                if (exceptions.Count > 0)
                {
                    throw new AggregateException(exceptions).Flatten();
                }
            }

            await Task.WhenAll(pending);
        }