Your problem is that a program normally exits when its Main()
method finishes. And your Main()
finishes as soon as you hit the await
in Run()
, because that’s how async
methods work.
What you should do is to make Run()
into an async Task
method and then wait for the Task
in your Main()
method:
static void Main()
{
RunAsync().Wait();
}
private static async Task RunAsync()
{
…
}
In C# 7.1+ you should use async Main
instead:
static async Task Main()
{
await RunAsync();
}
private static async Task RunAsync()
{
…
}
Few more notes:
- You should never use
async void
methods, unless you have to (which is the case of async event handlers). - Mixing
await
andWait()
in a GUI application or in ASP.NET is dangerous, because it leads to deadlocks. But it’s the right solution if you want to useasync
in a console application.