When to use Task.WaitAll vs. Task.WhenAll in .NET

Understand the differences between Task.WaitAll and Task.WhenAll methods and when to use which in your application

When to use Task.WaitAll vs. Task.WhenAll in .NET

The TPL (Task Parallel Library) is one of the most interesting new features added in the recent versions of .NET framework. The Task.WaitAll and Task.WhenAll methods are two important and frequently used methods in the TPL.

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

So, if you are using Task.WhenAll you will get a task object that isn’t complete. However, it will not block but will allow the program to execute. On the contrary, the Task.WaitAll method call actually blocks and waits for all other tasks to complete.

Essentially, Task.WhenAll will give you a task that isn’t complete, but you can use ContinueWith as soon as the specified tasks have completed their execution. Note that neither Task.WhenAll nor Task.WaitAll will actually run the tasks; i.e., no tasks are started by these methods. Here is how ContinueWith is used with Task.WhenAll: 

Task.WhenAll(taskList).ContinueWith(t => {
  // write your code here

To continue reading this article register now

How to choose a low-code development platform