Joe*_*oel 2 c# wpf asynchronous async-await .net-4.5
如何在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();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
该UpdateTasklist()方法连接到wcf webservice并要求所有打开的工作项.在SubmitWorkitem()方法中提交的工作项目仍然是答复的一部分.我认为这是因为UpdateTasklist()在提交工作项目之前执行.
请注意,这UpdateTasklist()也是一种异步方法
重要提示:请勿ASYNC VOID编写方法(除非您正在编写事件处理程序)
对于其余的:
这已经是您的代码中发生的事情; 这就是await 意味着什么; 基本上,您的DifferentClass.UpdateTasklist();方法作为继承的一部分发生,当且仅当第一个task(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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1453 次 |
| 最近记录: |