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 ?
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.
WhenAll returns control after all tasks are completed, while WhenAny returns control as soon as a single task is completed.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With