Advertisement
Pocok5

TaskRun

Mar 28th, 2022
1,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Collections.Generic;
  4.  
  5. class Program {
  6.     private void WriteABunchOfStuff(int id){
  7.         int j=0;
  8.         for (int i=0; i<50; i++){
  9.             Console.WriteLine("{0}# task here!", id);
  10.             j++;
  11.         }
  12.     }
  13.  
  14.     public async Task Run(){
  15.         List<Task> tasks = new();
  16.  
  17.         for (int i=0; i<10; i++){
  18.             var tmp=i;
  19.             tasks.Add(Task.Run(()=>{WriteABunchOfStuff(tmp); Console.WriteLine("{0} Done", tmp);}));
  20.         }
  21.  
  22.         await Task.WhenAll(tasks);
  23.         Console.WriteLine("Done");
  24.     }
  25.  
  26.     public static async Task Main(){
  27.         var p = new Program();
  28.         await p.Run();
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement