Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Created November 12, 2017 14:28
Show Gist options
  • Save luisdeol/dd9b52f82e3538f638f2b18e72acc9c0 to your computer and use it in GitHub Desktop.
Save luisdeol/dd9b52f82e3538f638f2b18e72acc9c0 to your computer and use it in GitHub Desktop.
Using WaitAll on Multiple Tasks
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