Created
November 12, 2017 14:28
-
-
Save luisdeol/dd9b52f82e3538f638f2b18e72acc9c0 to your computer and use it in GitHub Desktop.
Using WaitAll on Multiple Tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MultiThreadingExamples | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Stopwatch myStopWatch = new Stopwatch(); | |
Task[] aSetOfTasks = new Task[3]; | |
myStopWatch.Start(); | |
aSetOfTasks[0] = Task.Run(() => | |
{ | |
Thread.Sleep(1000); | |
Console.WriteLine("Luis drinks coffee."); | |
}); | |
aSetOfTasks[1] = Task.Run(() => | |
{ | |
Thread.Sleep(1000); | |
Console.WriteLine("Luis fixes some bugs."); | |
}); | |
aSetOfTasks[2] = Task.Run(() => | |
{ | |
Thread.Sleep(1000); | |
Console.WriteLine("Luis... falls asleep."); | |
}); | |
Task.WaitAll(aSetOfTasks); | |
myStopWatch.Stop(); | |
var timeStampStopWatch = myStopWatch.Elapsed; | |
Console.WriteLine($"It took {timeStampStopWatch.ToString("g")}"); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment