Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. public static void DoWork(int i)
  2. {
  3. Console.WriteLine($"Task {i} starting");
  4. Thread.Sleep(2000);
  5. Console.WriteLine($"Task {i} finished");
  6. }
  7. public static void WaitAll()
  8. {
  9. Task[] tasks = new Task[10];
  10. for(int i=0; i<10; i++)
  11. {
  12. int taskNum = i; // makes a local copy of the loop counter so that the correct task number is
  13. //passed into the lambda expression.
  14. tasks[i] = Task.Run(() => DoWork(taskNum));
  15. }
  16. //Waits for all tasks to complete running
  17. Task.WaitAll(tasks);
  18. Console.WriteLine("Finished Processing. Press a key to end");
  19. Console.ReadKey();
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement