Created
April 17, 2016 09:37
-
-
Save Zolomon/87784184a22951b428ada22f926333d9 to your computer and use it in GitHub Desktop.
Running tasks in parallel
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
var spinner = new Action<int, string>((ms, name) => | |
{ | |
var start = DateTime.Now; | |
while (true) | |
{ | |
var now = DateTime.Now; | |
if ((now - start).Milliseconds > ms) | |
{ | |
Console.WriteLine($"[{name}]"); | |
start = now; | |
} | |
Thread.Sleep((now - start).Milliseconds); | |
} | |
}); | |
var t1 = Task.Run(async () => spinner(750, "750")); | |
var t2 = Task.Run(async () => spinner(1000, "1000")); | |
var t3 = Task.Run(async () => spinner(2000, "2000")); | |
await Task.WhenAll(t1, t2, t3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment