using System.Collections.Generic;
using System.Threading.Tasks;
PackageView packageView = null;
var executes = new List<Func<Task>>(2);
executes.Add(new Func<Task>(async () =>
await CreateAuditAccess();
executes.Add(new Func<Task>(async () =>
packageView = await GetPackageView();
WhenAll(executes).GetAwaiter().GetResult();
Console.WriteLine("All went well");
public static async Task CreateAuditAccess()
await Task.Delay(5000).ConfigureAwait(false);
public static async Task<PackageView> GetPackageView()
await Task.Delay(5000).ConfigureAwait(false);
throw new Exception("PackageView Exception");
return new PackageView();
public static async Task WhenAll(List<Func<Task>> execute, byte concurrency = 50)
var taskBuffer = new List<Task>(concurrency);
if (execute.Count <= concurrency)
foreach (var executor in execute)
taskBuffer.Add(executor());
await Task.WhenAll(taskBuffer).ConfigureAwait(false);
int inputPosition = concurrency;
for (int index = 0; index < concurrency; index++)
taskBuffer.Add(execute[index]());
while (execute.Count > completed)
var completedTask = await Task.WhenAny(taskBuffer).ConfigureAwait(false);
taskBuffer.Remove(completedTask);
if (inputPosition < execute.Count)
taskBuffer.Add(execute[inputPosition]());
public bool Result {get; set; }
public Exception Exception {get; set; }