所以我试图让我的头在.net 4.5中这个新的'异步'的东西。我先前播放一点与异步控制器和任务并行库,并与这一段代码卷起:MVC4异步和并行执行
借此模型:
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秒。在翻译的某处,代码不再平行。我知道异步和并行是两个不同的概念,但不知何故,我认为代码将工作相同。有人能指出这里发生了什么以及如何修复?
所以基本上我混淆了异步和平行。我(错误地)认为当一个任务的结果最终被使用时会发生实际的等待(与linq查询不同,只有当它被枚举时才会执行)。 – Lodewijk 2012-01-31 12:08:40