Task.WhenAll - Task.WaitAll

Markus Freitag 3,791 Reputation points
2023-07-06T16:37:48.1533333+00:00

Hello,

what is better?

Task.WhenAll(task1, task2);   Task.WaitAll(task1, task2);

Or is

await Task.WhenAll(task1, task2); Task.WaitAll(task1, task2);

the same?

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
Waiting for tasks to complete.
Task 2 complete
Task 1 - iteration 0
Task 1 - iteration 1
Task 1 - iteration 2
Task 1 - iteration 3
Task 1 - iteration 4
Task 1 complete
**Tasks Completed.**


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();
      }
   }
}


Waiting for tasks to complete.
Task 2 complete
Task 1 - iteration 0
**Tasks Completed.**
Task 1 - iteration 1
Task 1 - iteration 2
Task 1 - iteration 3
Task 1 - iteration 4
Task 1 complete
Developer technologies | C#
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-07-07T19:49:25.4+00:00

    WhenAll() returns an awaitable Task, while Wait()/WaitAll() suspends the current thread.

    as Task.WhenAll() return a Task, your sample sync code should:

        Task.WhenAll(task1, task2).Wait();
    

    a better example using WhenAll():

    using System;
    using System.Threading.Tasks;
    					
    public class Program
    {
    	public static void Main()
    	{
    		Process().Wait();
    	}
    	static async Task Process()
    	{
             var task1 = Task.Run(() =>{
                for (int i = 0; i < 5; i++){
                   Console.WriteLine("Task 1 - iteration {0}", i);
                   Task.Delay(1000);
                }
                Console.WriteLine("Task 1 complete");
             });
             var task2 = Task.Run(() =>{
                Console.WriteLine("Task 2 complete");
             });
             Console.WriteLine("Waiting for tasks to complete.");
             await Task.WhenAll(task1, task2);
             Console.WriteLine("Tasks Completed.");	}
    }
    >>>>
    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.
    
    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-07-07T07:21:24.4333333+00:00

    Hi @Markus Freitag , Welcome to Microsoft Q&A.

    In fact, there is no good or bad, it mainly depends on your usage scenario.

    The await keyword is used in an asynchronous environment. If you have asynchronous usage requirements, you need to choose to use it.

    Task.WaitAll will block the current process,

    • This method blocks the current thread until all the provided tasks have completed.
    • In the first code example, the program will print "Waiting for tasks to complete." and then block the main thread until both task1 and task2 are completed.
    • After both tasks have completed, it will print "Tasks Completed." and continue with the program execution.

    If there is no need for async, I would choose to use Task.WaitAll(task1, task2);

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.