Home > Backend Development > C#.Net Tutorial > How to run multiple asynchronous tasks in C# and wait for them all to complete?

How to run multiple asynchronous tasks in C# and wait for them all to complete?

PHPz
Release: 2023-08-27 11:17:07
forward
1503 people have browsed it

如何在 C# 中运行多个异步任务并等待它们全部完成?

Task.WaitAll blocks the current thread until all other tasks have completed execution.

Task.WhenAll method is used to create a task that will complete if and only if all other tasks have completed. In the first example, we can see that when using Task.WhenAll, task completion occurs before other tasks complete. This means that Task.WhenAll will not block execution. In the second example, we can see that when using Task.WaitAll, task completion is only executed after all other tasks have completed. This means that Task.WaitAll blocks execution.

Example

static void Main(string[] args){
   Task task1 = new Task(() =>{
      for (var i = 0; i < 5; i++){
         Console.WriteLine("Task 1 - iteration {0}", i);
         Task.Delay(1000);
      }
      Console.WriteLine("Task 1 complete");
   });
   Task task2 = new Task(() =>{
      for (var i = 0; i < 5; i++){
         Console.WriteLine("Task 2 - iteration {0}", i);
         Task.Delay(1000);
      }
      Console.WriteLine("Task 2 complete");
   });
   task1.Start();
   task2.Start();
   Console.WriteLine("Waiting for tasks to complete.");
   Task.WhenAll(task1, task2);
   Console.WriteLine("Both Tasks Completed.");
   Console.ReadLine();
}
Copy after login

Output

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

Example

static void Main(string[] args){
   Task task1 = new Task(() =>{
      for (var i = 0; i < 5; i++){
         Console.WriteLine("Task 1 - iteration {0}", i);
         Task.Delay(1000);
      }
      Console.WriteLine("Task 1 complete");
   });
   Task task2 = new Task(() =>{
      for (var i = 0; i < 5; i++){
         Console.WriteLine("Task 2 - iteration {0}", i);
         Task.Delay(1000);
      }
      Console.WriteLine("Task 2 complete");
   });
   task1.Start();
   task2.Start();
   Console.WriteLine("Waiting for tasks to complete.");
   Task.WaitAll(task1, task2);
   Console.WriteLine("Both Tasks Completed.");
   Console.ReadLine();
}
Copy after login

Output

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

The above is the detailed content of How to run multiple asynchronous tasks in C# and wait for them all to complete?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template