.NET 4 equivalent of Task.WhenAll()

In .NET 4, is there any functional equivalent to .NET 4.5's System.Threading.Tasks.Task.WhenAll()?

The goal is to wrap up multiple async tasks into a single one that is completed when all of its constituent tasks are done.

Answers


I think the closest thing built-in in .Net 4.0 is ContinueWhenAll(). You can make the continuationAction a simple tasks => tasks and use the returned Task.

For performance reasons, you might want to use it with TaskContinuationOptions.ExecuteSynchronously.


In .NET Framework 4.0 WhenAll and WhenAny can be used with installed AsyncCTP in case of Visual Studio 2010 or Async Targeting Pack in case of Visual Studio 2012.

The WhenAll and WhenAny methods are then offered on the TaskEx type.


Try waiting on Task.WaitAll() in another Task. Use the LINQ extension method ToArray to convert from IEnumerable<Task> to Task[].

Task WhenAll(IEnumerable<Task> tasks)
{
    return Task.Factory.StartNew(() => Task.WaitAll(tasks.ToArray()));
}

Need Your Help

Using an object as key for NSDictionary

iphone objective-c ios

I have a bunch of Lessons and the class works great. There is a view controller that works with these lessons. This controller needs to know the upload status of a lesson so we have a NSDictionary ...

Why use INotifyPropertyChanged with bindings in WPF?

c# wpf binding

I've notived that practically every example I find on the internet about bindings has a class (which binds to another property) that inherits the INotifyPropertyChanged interface and uses a method ...