I know it is bad to call Task.Wait in UI thread. It causes a deadlock. Please refer to
Constructor invoke async method
Await, and UI, and deadlocks! Oh my!
Take the following code:
public MainPage()
{
this.InitializeComponent();
step0();
step1();
}
private async Task step0()
{
await Task.Delay(5000);
System.Diagnostics.Debug.WriteLine("step 0");
}
private async Task step1()
{
await Task.Delay(3000);
System.Diagnostics.Debug.WriteLine("step 1");
}
}
How can I ensure that “step 0” is always being printed before “step 1”? Using Task.Wait would cause deadlock. This is Windows Store App
You can use
Task.ContinueWithto chain the tasks together so that they happen sequentially.Also, Alexei’s comment is correct.
Task.Waitwill block the UI thread. Deadlocks are something else.