所以我试图让我的头脑围绕这个新的“异步”东西在.net 4.5.我以前玩异步控制器和任务并行库,并清理了这段代码:
采取这种模式:
public class TestOutput { public string One { get; set; } public string Two { get; set; } public string Three { get; set; } public static string DoWork(string input) { Thread.Sleep(2000); return input; } }
在这样的控制器中使用哪一个:
public void IndexAsync() { AsyncManager.OutstandingOperations.Increment(3); Task.Factory.StartNew(() => { return TestOutput.DoWork("1"); }) .ContinueWith(t => { AsyncManager.OutstandingOperations.Decrement(); AsyncManager.Parameters["one"] = t.Result; }); Task.Factory.StartNew(() => { return TestOutput.DoWork("2"); }) .ContinueWith(t => { AsyncManager.OutstandingOperations.Decrement(); AsyncManager.Parameters["two"] = t.Result; }); Task.Factory.StartNew(() => { return TestOutput.DoWork("3"); }) .ContinueWith(t => { AsyncManager.OutstandingOperations.Decrement(); AsyncManager.Parameters["three"] = t.Result; }); } public ActionResult IndexCompleted(string one,string two,string three) { return View(new TestOutput { One = one,Two = two,Three = three }); }
由于TPL的魔力,该控制器会在2秒内呈现视图.
现在,我预计(相当天真地),上面的代码将转换为以下内容,使用C#5的新“异步”和“等待”功能:
public async Task<ActionResult> Index() { return View(new TestOutput { One = await Task.Run(() =>TestOutput.DoWork("one")),Two = await Task.Run(() =>TestOutput.DoWork("two")),Three = await Task.Run(() =>TestOutput.DoWork("three")) }); }
该控制器在6秒内呈现视图.在翻译的某个地方,代码变得不再平行.我知道异步和并行是两个不同的概念,但不知何故我认为代码将工作相同.有人可以指出这里发生了什么,如何解决?
解决方法
Somewhere in the translation the code became no longer parallel.
恰恰.等待(异步)等待单个操作完成.
并行异步操作可以通过启动实际的任务来完成,但不等待它们直到以后:
public async Task<ActionResult> Index() { // Start all three operations. var tasks = new[] { Task.Run(() =>TestOutput.DoWork("one")),Task.Run(() =>TestOutput.DoWork("two")),Task.Run(() =>TestOutput.DoWork("three")) }; // Asynchronously wait for them all to complete. var results = await Task.WhenAll(tasks); // Retrieve the results. return View(new TestOutput { One = results[0],Two = results[1],Three = results[2] }); }
附:还有一个Task.WhenAny.