Skip to content

Instantly share code, notes, and snippets.

@phillijw
Last active May 25, 2017 15:38
Show Gist options
  • Save phillijw/679cf0d9af6dee41d23c3fc4b839c15f to your computer and use it in GitHub Desktop.
Save phillijw/679cf0d9af6dee41d23c3fc4b839c15f to your computer and use it in GitHub Desktop.
simple async/await example
Console.WriteLine(DateTime.Now);
//This block takes 1 second to run because all 5 tasks are running simultaneously
{
var a = Task.Delay(1000);
var b = Task.Delay(1000);
var c = Task.Delay(1000);
var d = Task.Delay(1000);
var e = Task.Delay(1000);
await a;
await b;
await c;
await d;
await e;
}
Console.WriteLine(DateTime.Now);
//This block takes 5 seconds to run because each "await" pauses the program until the task finishes
{
await Task.Delay(1000);
await Task.Delay(1000);
await Task.Delay(1000);
await Task.Delay(1000);
await Task.Delay(1000);
}
Console.WriteLine(DateTime.Now);
OUTPUT:
5/24/2017 2:22:50 PM
5/24/2017 2:22:51 PM
5/24/2017 2:22:56 PM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment