I am trying to write a version of Task.WhenAll
for .NET 3.5 using the "Task Parallel Library for .NET 3.5". This is what I came up with. Is there a better way of doing this?
public static Task WhenAll(IEnumerable<Task> tasks)
{
var tcs = new TaskCompletionSource<object>();
var remainingTasks = tasks.ToList();
int count = remainingTasks.Count();
var exceptions = new List<Exception>();
foreach (var task in remainingTasks)
{
task.ContinueWith(t =>
{
if (Interlocked.Decrement(ref count) == 0)
{
foreach (var task1 in remainingTasks)
{
if (task1.IsFaulted)
{
exceptions.Add(task1.Exception);
}
}
if (exceptions.Any())
{
tcs.SetException(new AggregateException(exceptions));
}
else
{
tcs.SetResult(null);
}
}
});
}
return tcs.Task;
}