[英]wait for a Task that calls an async method to complete without blocking thread
[英]Wait for async method without blocking the thread
如何在UpdateTasklist()
方法之后执行SubmitWorkitem()
方法而不阻塞线程?
private async void SubmitWorkitem(Workitem workitem)
{
await Task.Run(() => this.SubmitWorkitem(workitem));
//UpdateTasklist() should be executed after SubmitWorkitem() method.
//How can i achieve this without blocking the UI thread?
var locator = new ViewModelLocator();
locator.Task.UpdateTasklist();
}
编辑:
UpdateTasklist()
方法连接到wcf Web服务,并要求所有打开的工作项。 在SubmitWorkitem()
方法中提交的工作项仍然是答复的一部分。 我认为那是因为UpdateTasklist()
在提交工作项之前已执行。
请注意, UpdateTasklist()
也是一个异步方法
重要提示: 请勿编写ASYNC VOID
方法 (除非您正在编写事件处理程序)
对于其余的:
这已经在您的代码中发生了; 这是await
意思 ; 基本上,您的DifferentClass.UpdateTasklist();
方法仅在第一个任务( this.SubmitWorkitem(workitem)
)完成时才作为连续调用的一部分发生。
有了您的编辑, 存在丢失的一步:你应该await
第二个方法,否则该方法不能报告完成/失败(IIRC编译器也会唠叨你):
private async Task SubmitWorkitem(Workitem workitem)
{
await Task.Run(() => this.SubmitWorkitem(workitem));
var locator = new ViewModelLocator();
await locator.Task.UpdateTasklist();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.