Last active
February 27, 2019 21:12
-
-
Save M-Yankov/34ba1b569575c5bf9ea34d3845382637 to your computer and use it in GitHub Desktop.
The correct way two invoke two async methods
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
public class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
Stopwatch timer = Stopwatch.StartNew(); | |
Task a = OperationSlow(); | |
Task b = OperationFast(); | |
await Task.Delay(5000); | |
Console.WriteLine("Main"); | |
await a; | |
await b; | |
timer.Stop(); | |
Console.WriteLine(timer.Elapsed.TotalSeconds); // Total seconds is 5! | |
} | |
public static async Task OperationSlow() | |
{ | |
await Task.Delay(3000); | |
Console.WriteLine("Slow"); | |
} | |
public static async Task OperationFast() | |
{ | |
await Task.Delay(500); | |
Console.WriteLine("Fast"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment