在UI线程替代方案Task.Wait [英] Alternative for Task.Wait in UI thread
本文介绍了在UI线程替代方案Task.Wait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这是不好的叫 Task.Wait
在UI线程。它会导致死锁。请参考
I know it is bad to call Task.Wait
in UI thread. It causes a deadlock. Please refer to
<一个href=\"http://social.msdn.microsoft.com/Forums/en/winappswithcsharp/thread/0d24419e-36ad-4157-abb5-3d9e6c5dacf1\">Constructor调用异步方法
看看下面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");
}
}
我如何能确保踩0总是被第1步前打印?使用 Task.Wait
将导致死锁。 这是由于Windows Store应用
推荐答案
您可以让他们连续发生使用 Task.ContinueWith
来链的共同任务
You can use Task.ContinueWith
to chain the tasks together so that they happen sequentially.
public MainPage()
{
this.InitializeComponent();
step0().ContinueWith(t => step1());
}
此外,阿列克谢的评论是正确的。 Task.Wait
将块的UI线程。死锁是别的东西。
Also, Alexei's comment is correct. Task.Wait
will block the UI thread. Deadlocks are something else.
这篇关于在UI线程替代方案Task.Wait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文