Created
December 17, 2017 21:17
-
-
Save sjehutch/4cca3f8f73a06bf213463c673a2d00ba to your computer and use it in GitHub Desktop.
async await c#
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
| 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment