我可能错过了一些东西,但做的和做的有什么区别:
public void MyMethod()
{
Task t = Task.Factory.StartNew(DoSomethingThatTakesTime);
t.Wait();
UpdateLabelToSayItsComplete();
}
public async void MyMethod()
{
var result = Task.Factory.StartNew(DoSomethingThatTakesTime);
await result;
UpdateLabelToSayItsComplete();
}
private void DoSomethingThatTakesTime()
{
Thread.Sleep(10000);
}
Wait(),将导致以同步方式运行潜在的异步代码。等待不会。
For example, you have an asp.net web application. UserA calls /getUser/1 endpoint. asp.net app pool will pick a thread from thread pool (Thread1) and, this thread will make a http call. If you do Wait(), this thread will be blocked until http call resolves. While it is waiting, if UserB calls /getUser/2, then, app pool will need to serve another thread (Thread2) to make http call again. You just created (Well, fetched from app pool actually) another thread for no reason, because you cannot use Thread1 it was blocked by Wait().
如果你在Thread1上使用await,那么SyncContext将管理Thread1和http调用之间的同步。简单地说,它将在http调用完成后通知。同时,如果UserB调用/getUser/2,那么,你将再次使用Thread1进行http调用,因为它在await被命中时被释放。然后另一个请求可以使用它,甚至更多。一旦http调用完成(user1或user2), Thread1就可以获得结果并返回给调用方(客户端)。Thread1用于多个任务。