What is the difference between Task.WhenAll() and Task.WaitAll() in C#?

CsharpServer Side ProgrammingProgramming

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.

If we are using Task.WhenAll we 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.

To understand with an example, let us say we have a task that performs some activity with the UI thread say some animation needs to be shown in the user interface. Now, if we use Task.WaitAll, the user interface will be blocked and will not be updated until all the related tasks are completed and the block released. However, if we are using Task.WhenAll in the same application, the UI thread will not be blocked and would be updated as usual.

Example for Task.WhenAll

Example

 Live Demo

using System;
using System.Threading.Tasks;
namespace DemoApplication{
   public class Program{
      static void Main(string[] args){
         Task task1 = new Task(() =>{
            for (int i = 0; i < 5; i++){
               Console.WriteLine("Task 1 - iteration {0}", i);
               Task.Delay(1000);
            }
            Console.WriteLine("Task 1 complete");
         });
         Task task2 = new Task(() =>{
            Console.WriteLine("Task 2 complete");
         });
         task1.Start();
         task2.Start();
         Console.WriteLine("Waiting for tasks to complete.");
         Task.WhenAll(task1, task2);
         Console.WriteLine("Tasks Completed.");
         Console.ReadLine();
      }
   }
}

Output

The output of the above code is

Waiting for tasks to complete.
Tasks Completed.
Task 1 - iteration 0
Task 2 complete
Task 1 - iteration 1
Task 1 - iteration 2
Task 1 - iteration 3
Task 1 - iteration 4
Task 1 complete

In the above example we could see that when using Task.WhenAll the task complete is executed before the other tasks are completed. This means that Task.WhenAll doesn’t block the execution.

Example for Task.WaitAll

Example

 Live Demo

using System;
using System.Threading.Tasks;
namespace DemoApplication{
   public class Program{
      static void Main(string[] args){
         Task task1 = new Task(() =>{
            for (int i = 0; i < 5; i++){
               Console.WriteLine("Task 1 - iteration {0}", i);
               Task.Delay(1000);
            }
            Console.WriteLine("Task 1 complete");
         });
         Task task2 = new Task(() =>{
            Console.WriteLine("Task 2 complete");
         });
         task1.Start();
         task2.Start();
         Console.WriteLine("Waiting for tasks to complete.");
         Task.WaitAll(task1, task2);
         Console.WriteLine("Tasks Completed.");
         Console.ReadLine();
      }
   }
}

Output

The output of the above code is

Waiting for tasks to complete.
Task 1 - iteration 0
Task 2 complete
Task 1 - iteration 1
Task 1 - iteration 2
Task 1 - iteration 3
Task 1 - iteration 4
Task 1 complete
Tasks Completed.

In the above example we could see that when using Task.WaitAll the task complete is executed only after all the other tasks are completed. This means that Task.WaitAll block the execution.

raja
Updated on 19-Aug-2020 11:40:59

Advertisements