C# System.Threading.Tasks->;延续
假设您拥有以下服务:C# System.Threading.Tasks->;延续,c#,.net,task,task-parallel-library,C#,.net,Task,Task Parallel Library,假设您拥有以下服务: interface ISuessService { Task<Thing> Thing1(); Task<Thing> Thing2(); } 并与UI线程轻松交互 使用扩展方法实现新的continue的最佳方法是什么: myService.Thing1() .ContinueWith(myService.Thing2()) .ContinueOnUIThread(_ => label.Text = "Done!")
interface ISuessService {
Task<Thing> Thing1();
Task<Thing> Thing2();
}
并与UI线程轻松交互
使用扩展方法实现新的continue的最佳方法是什么:
myService.Thing1()
.ContinueWith(myService.Thing2())
.ContinueOnUIThread(_ => label.Text = "Done!");
基本上在Thing1
完成后启动Thing2
,然后调用UI线程
最近的一次是这样的,但我真的不喜欢打电话给等等:
myService.Thing1()
.ContinueWith(_ => {
var thing2 = myService.Thing2().Wait();
return thing2.Result;
})
.ContinueOnUIThread(_ => label.Text = "Done!");
有没有干净的方法可以做到这一点
PS-我没有.NET4.5,所以不允许使用wait/async-此代码必须在Android的MonoTouch/Mono上运行,所以请坚持使用4.0
PS-请注意我使用的。
,这只是“我没有真正使用此参数”的快捷方式。我想您可能要查找的是
我只想使用一个普通的ContinueWith
和同步上下文重载,并从UI线程中为其提供同步上下文。我不明白:您可以将同步上下文传递给ContinueWith,那么有什么问题吗?Thing2
不需要UI线程,它应该在后台运行。
myService.Thing1()
.ContinueWith(_ => {
var thing2 = myService.Thing2().Wait();
return thing2.Result;
})
.ContinueOnUIThread(_ => label.Text = "Done!");
myService.Thing1()
.ContinueWith(_ => myService.Thing2()).Unwrap()
.ContinueOnUIThread(_ => label.Text = "Done!");