Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void DoWork(int i)
- {
- Console.WriteLine($"Task {i} starting");
- Thread.Sleep(2000);
- Console.WriteLine($"Task {i} finished");
- }
- public static void WaitAll()
- {
- Task[] tasks = new Task[10];
- for(int i=0; i<10; i++)
- {
- int taskNum = i; // makes a local copy of the loop counter so that the correct task number is
- //passed into the lambda expression.
- tasks[i] = Task.Run(() => DoWork(taskNum));
- }
- //Waits for all tasks to complete running
- Task.WaitAll(tasks);
- Console.WriteLine("Finished Processing. Press a key to end");
- Console.ReadKey();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement