如何以特定顺序执行异步任务 [英] How to execute async tasks in a specific order
问题描述
我有两项任务,我想在下一项开始之前完成其中一项.我试图将它们变成一个任务并使用task.Wait(),但是它们仍然在错误的时间执行.短暂的睡眠是可行的,但我想避免睡眠.
I have two tasks, one of which I would like to complete before the next one begins. I have tried turning these into a task and using task.Wait() but they are still executed at the wrong time. Using a short sleep works but I would like to avoid sleeping.
private async void BuildNextData(object sender, RoutedEventArgs e)
{
try
{
await this._LocalCluster.DataItem(this._LocalSceneIID.IID.Value, ".Data.DataPopupNext", null, null, null);
System.Threading.Thread.Sleep(500);
object o = await GetVentuxIndex(".Data.DataPopupCurrentIndex");
Workspace.This.MainJsonBond.SelectedfilteredPopupWorries = Workspace.This.MainJsonBond.filteredPopupWorries[Int32.Parse(o.ToString())];
}
catch (Exception ex)
{
}
}
推荐答案
除了没有 Sleep()
以外,这就是您的正确方法. await
的意思是等待此操作完成再继续".
That's exactly the way you would do it, except without the Sleep()
. await
means, "wait for this to complete before continuing".
如果 DataItem
方法正在执行的发送和接收"在停止等待后还没有完全完成,那么这就是该方法中代码的错误.您将必须与负责该主机应用程序的人员联系.您实际上无能为力.那不是应该等待的任务应该起作用的方式.
If the "sending and receiving" that the DataItem
method is doing is not fully complete once it stops awaiting, then that's the fault of the code in that method. You will have to take that up with whoever is responsible for that host application. There isn't really anything you can do. That is not the way awaitable tasks are supposed to work.
这篇关于如何以特定顺序执行异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!