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.ContinueWith
to chain the tasks together so that they happen sequentially.
public MainPage()
{
this.InitializeComponent();
step0().ContinueWith(t => step1());
}
Also, Alexei's comment is correct. Task.Wait
will block the UI thread. Deadlocks are something else.