Skip to content

Instantly share code, notes, and snippets.

@M-Yankov
Last active February 27, 2019 21:12
Show Gist options
  • Save M-Yankov/34ba1b569575c5bf9ea34d3845382637 to your computer and use it in GitHub Desktop.
Save M-Yankov/34ba1b569575c5bf9ea34d3845382637 to your computer and use it in GitHub Desktop.
The correct way two invoke two async methods
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