I am failing to understand why calling a async method from the Main method from a console application hangs forever. I feel I am missing some key element of asynchronous execution:
static void Main(string[] args)
{
Console.WriteLine("w3");
var exe = new Exe2();
exe.Do2().Wait();
Console.WriteLine("/w3");
Console.ReadKey();
}
public class Exe2
{
public async Task Do2()
{
Task task1 = new Task(() => { Console.WriteLine("t1"); Task.Delay(2000); });
Task task2 = new Task(() => { Console.WriteLine("t2"); Task.Delay(2000); });
Task task3 = new Task(() => { Console.WriteLine("t3"); Task.Delay(2000); });
await Task.WhenAll(task1, task2, task3);
}
}
The code above prints w3 not nothing else, and doesn't take the ReadKey
either.