Coder Perfect

WaitAll vs WhenAll

Problem

What is the difference between a task and a project? Task.WhenAll() and WaitAll() from the Async CTP? Could you please supply some sample code to demonstrate the various use cases?

Asked by Yaron Levi

Solution #1

Task. Until everything is finished, WaitAll blocks the current thread.

When you use Task.WhenAll, you’ll get a task that represents the action of waiting until everything is finished.

As a result, you can utilize the following from an async method:

await Task.WhenAll(tasks);

…which means your approach will continue once everything is finished, but you won’t tie up a thread to wait till then.

Answered by Jon Skeet

Solution #2

While JonSkeet’s answer does a good job of explaining the differences, there is one more: exception handling.

Task. When any of the jobs throws an AggregateException, WaitAll throws an AggregateException, and you can inspect all thrown exceptions. The Task in which you must wait. The AggregateException is unwrapped by WhenAll, which only returns the first exception.

When the program below is run with await Task, it will look like this. The output of WhenAll(taskArray) 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.

The output of the program below when run with Task.WaitAll(taskArray) 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();
    }
}

Answered by tymtam

Solution #3

For example, if you have a task that interacts with the UI thread (for example, a task that represents an animation in a Storyboard), if you Task. The UI thread is then halted, and the UI is never updated. if you utilize the await Task method. The UI thread is not blocked when WhenAll() is called, and the UI is updated.

Answered by J. Long

Solution #4

What exactly do they do?

What’s the difference:

Use which when:

Answered by i100

Post is based on https://stackoverflow.com/questions/6123406/waitall-vs-whenall