Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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.

like image 575
kpozin Avatar asked Jul 16 '12 21:07

kpozin


People also ask

What is task WhenAll in C#?

WhenAll(IEnumerable<Task>) Creates a task that will complete when all of the Task objects in an enumerable collection have completed. WhenAll(Task[]) Creates a task that will complete when all of the Task objects in an array have completed.

Can you tell difference between task WhenAll and task WhenAny?

WhenAll returns control after all tasks are completed, while WhenAny returns control as soon as a single task is completed.

Does task WhenAll start the tasks?

WhenAll creates a task that will complete when all of the supplied tasks have been completed. It's pretty straightforward what this method does, it simply receives a list of Tasks and returns a Task when all of the received Tasks completes.

Does task WhenAll create new thread?

WhenAll does not create a new thread. A "task" does not necessarily imply a thread; there are two types of tasks: "event" tasks (e.g., TaskCompletionSource ) and "code" tasks (e.g., Task. Run ). WhenAll is an event-style task, so it does not represent code.


1 Answers

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.

like image 114
Martin Suchan Avatar answered Sep 20 '22 15:09

Martin Suchan