Consider this,
Task task = new Task (async () =>{
await TaskEx.Delay(1000);
});
task.Start();
task.Wait();
The call task.Wait() does not wait for the task completion and the next line is executed immediately, but if I wrap the async lambda expression into a method call, the code works as expected.
private static async Task AwaitableMethod()
{
await TaskEx.Delay(1000);
}
then (updated according comment from svick)
await AwaitableMethod();
AwaitableMethod
you are actually returning and calling Wait on the task returned from the .Delay() method (I'm assuming it returns aTask
). In the async lambda you are calling Wait on theTask task
. But still, I have no explanation. – Crippenawait
withWait()
. In many cases, that can lead to deadlocks. – Autarchawait
withWait()
– Boer