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 ?
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.
While JonSkeet's answer explains the difference in a typically excellent way there is another difference: exception handling.
Task.WaitAll
throws an AggregateException
when any of the tasks throws and you can examine all thrown exceptions. The await
in await Task.WhenAll
unwraps the AggregateException
and 'returns' only the first exception.
When the program below executes with await Task.WhenAll(taskArray)
the output is as follows.
19/11/2016 12:18:37 AM: Task 1 started
19/11/2016 12:18:37 AM: Task 3 started
19/11/2016 12:18:37 AM: Task 2 started
Caught Exception in Main at 19/11/2016 12:18:40 AM: Task 1 throwing at 19/11/2016 12:18:38 AM
Done.
When the program below is executed with Task.WaitAll(taskArray)
the output is as follows.
19/11/2016 12:19:29 AM: Task 1 started
19/11/2016 12:19:29 AM: Task 2 started
19/11/2016 12:19:29 AM: Task 3 started
Caught AggregateException in Main at 19/11/2016 12:19:32 AM: Task 1 throwing at 19/11/2016 12:19:30 AM
Caught AggregateException in Main at 19/11/2016 12:19:32 AM: Task 2 throwing at 19/11/2016 12:19:31 AM
Caught AggregateException in Main at 19/11/2016 12:19:32 AM: Task 3 throwing at 19/11/2016 12:19:32 AM
Done.
The program:
class MyAmazingProgram
{
public class CustomException : Exception
{
public CustomException(String message) : base(message)
{ }
}
static void WaitAndThrow(int id, int waitInMs)
{
Console.WriteLine($"{DateTime.UtcNow}: Task {id} started");
Thread.Sleep(waitInMs);
throw new CustomException($"Task {id} throwing at {DateTime.UtcNow}");
}
static void Main(string[] args)
{
Task.Run(async () =>
{
await MyAmazingMethodAsync();
}).Wait();
}
static async Task MyAmazingMethodAsync()
{
try
{
Task[] taskArray = { Task.Factory.StartNew(() => WaitAndThrow(1, 1000)),
Task.Factory.StartNew(() => WaitAndThrow(2, 2000)),
Task.Factory.StartNew(() => WaitAndThrow(3, 3000)) };
Task.WaitAll(taskArray);
//await Task.WhenAll(taskArray);
Console.WriteLine("This isn't going to happen");
}
catch (AggregateException ex)
{
foreach (var inner in ex.InnerExceptions)
{
Console.WriteLine($"Caught AggregateException in Main at {DateTime.UtcNow}: " + inner.Message);
}
}
catch (Exception ex)
{
Console.WriteLine($"Caught Exception in Main at {DateTime.UtcNow}: " + ex.Message);
}
Console.WriteLine("Done.");
Console.ReadLine();
}
}
await t1; await t2; await t3;
vs await Task.WhenAll(t1,t2,t3);
await
, not a difference between the two methods. Both propagate an AggregateException
, either throwing directly or through a property (the Task.Exception
property).
As an example of the difference -- if you have a task the does something with the UI thread (e.g. a task that represents an animation in a Storyboard) if you Task.WaitAll()
then the UI thread is blocked and the UI is never updated. if you use await Task.WhenAll()
then the UI thread is not blocked, and the UI will be updated.
What do they do:
Internally both do the same thing.
What's the difference:
WaitAll is a blocking call
WhenAll - not - code will continue executing
Use which when:
WaitAll when cannot continue without having the result
WhenAll when what just to be notified, not blocked
WaitAll
as I understand it.
Task.WaitAll
after you did your some other work? I mean, instead of calling it right after starting your tasks.
The Task.WaitAll
blocks the current thread. It will remain blocked until all other tasks have completed execution. It has a void return value. The Task.WhenAll
method returns a Task<TResult[]>
. It is used to create a task that will complete if and only if all the other tasks have completed.
When to use which?
About the only time I use Task.WaitAll
is inside a non-async
function (that must remain non-async
), that I want to add concurrency to. However, BE WARNED: this can lead to deadlock since it blocks the current thread.
With that in mind, anytime you can convert a function to async
, do so, and use Task.WhenAll
, with an await
on it. This is definitely the preferred approach.
Exceptions
Task.WaitAll
throws an AggregateException
when any of the tasks throws and you can examine all thrown exceptions. The await
in await Task.WhenAll
unwraps the AggregateException
and 'returns' only the first exception. In both cases, all tasks will run, even if one of them throws an exception.
Success story sharing
WhenAll
, but that's not the same as blocking the thread.