Task.WaitAll vs Task.WhenAll

9/11/2020

First a quick explanation of the differences between Task.WaitAll and Task.WhenAll. 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 (see below). However BE WARNED: this can lead to deadlock since it blocks the current thread (see here).

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.

A working example of Task.WaitAll

If you do have a function that is making multiple web-service calls, and you cannot convert to async, you can take advantage of Task.Run and Task.WaitAll. Here's an example of what that looks like:

public ResultData GetResults(int id)
{
    var result = new ResultData();
    Task<Group1Data> group1DataTask = Task.Run(() => getGroup1Data(id));
    Task<Group2Data> group2DataTask = Task.Run(() => getGroup2Data(id));
    Task<Group3Data> group3DataTask = Task.Run(() => getGroup3Data(id));
    Task.WaitAll(group1DataTask , getGroup2DataTask , getGroup3DataTask );

    result.Group1 = group1DataTask.Result;
    result.Group2 = group2DataTask.Result;
    result.Group3 = group3DataTask.Result;
    return resultData;
}

Be sure to test this well. Again, Task.WaitAll blocks the current thread, and if not used in the proper context, this can block. On my current project, we do have code like this (using the old-school synchronous HttpWebRequest and it has been working fine, even in prod.


Please register or login to add a comment.

Comments (displaying 1 - 1):
No comments yet! Be the first...


  • C#/.NET/Core
  • T-SQL
  • HTML/Javascript/jQuery
  • ASP.NET/MVC
  • .NET Core
  • ADO.NET/EF
  • WPF
  • Xamarin/MAUI
  • Windows 10
  • SQL Server 20xx
  • Android
  • XBox One
  • Skiing
  • Rock Climbing
  • White water kayaking
  • Road Biking