Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WaitAll vs WhenAll

What is the difference between Task.WaitAll() and Task.WhenAll() from the Async CTP ? Can you provide some sample code to illustrate the different use cases ?

like image 733
Yaron Levi Avatar asked May 25 '11 11:05

Yaron Levi


People also ask

What is the difference between WaitAll and WhenAll?

WaitAll blocks the current thread until all other tasks have completed execution. The Task. WhenAll method is used to create a task that will complete if and only if all the other tasks 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.

What is task WhenAll?

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.

What is task WaitAll?

WaitAll(Task[], TimeSpan) Waits for all of the provided cancellable Task objects to complete execution within a specified time interval. WaitAll(Task[], Int32, CancellationToken) Waits for all of the provided Task objects to complete execution within a specified number of milliseconds or until the wait is cancelled.


1 Answers

Task.WaitAll blocks the current thread until everything has completed.

Task.WhenAll returns a task which represents the action of waiting until everything has completed.

That means that from an async method, you can use:

await Task.WhenAll(tasks); 

... which means your method will continue when everything's completed, but you won't tie up a thread to just hang around until that time.

like image 93
Jon Skeet Avatar answered Sep 21 '22 23:09

Jon Skeet