.NET 4.5和VS2012是我的目标
在我的C#中,我有很多旧代码:
var stuff;
ThreadPool.QueueUserWorkItem(() =>
{
stuff=GetStuff():
InvokeOnMainThread(stuff);
});如何在C#中使用新的任务系统完成这一任务?
发布于 2013-10-21 17:18:07
这通常会映射到:
Task.Factory.StartNew(() =>
{
return GetStuff():
}).ContinueWith(t =>
{
// InvokeOnMainThread(t.Result); // Note that this doesn't need to "Invoke" now
UseStuff(t.Result);
}, TaskScheduler.FromCurrentSynchronizationContext()); // Moves to main thread如果您使用的是Visual 2012和.NET 4.5,也可以选择标记async方法,并执行以下操作:
var stuff = await Task.Run(() => GetStuff());
UseStuff(stuff); // Will be on the main thread here...https://stackoverflow.com/questions/19500778
复制相似问题