Task.Start / Wait和Async / Await有什么区别?

Task.Start / Wait和Async / Await有什么区别?

我可能会遗漏一些东西但是做什么之间有什么不同:

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);}


呼如林
浏览 1628回答 3
3回答

饮歌长啸

我可能会遗漏一些东西你是。做Task.Wait和有await task什么区别?你在餐厅的服务员点了午餐。在给出订单后的一刻,一位朋友走进来,坐在你身边,开始交谈。现在你有两个选择。你可以忽略你的朋友,直到任务完成 - 你可以等到你的汤到来,在你等待的时候什么都不做。或者你可以回复你的朋友,当你的朋友停止说话时,服务员会给你带来汤。Task.Wait阻止任务完成 - 在任务完成之前,您将忽略您的朋友。await继续处理消息队列中的消息,当任务完成时,它会将一条消息列入“在等待之后从中断处继续”。你和你的朋友谈话,当谈话休息时,汤就到了。

DIEA

为了演示Eric的答案,这里有一些代码:public void ButtonClick(object sender, EventArgs e){   Task t = new Task.Factory.StartNew(DoSomethingThatTakesTime);   t.Wait();     //If you press Button2 now you won't see anything in the console    //until this task is complete and then the label will be updated!   UpdateLabelToSayItsComplete();}public async void ButtonClick(object sender, EventArgs e){   var result = Task.Factory.StartNew(DoSomethingThatTakesTime);   await result;   //If you press Button2 now you will see stuff in the console and    //when the long method returns it will update the label!   UpdateLabelToSayItsComplete();}public void Button_2_Click(object sender, EventArgs e){   Console.WriteLine("Button 2 Clicked");}private void DoSomethingThatTakesTime(){   Thread.Sleep(10000);}

小怪兽爱吃肉

这个例子非常清楚地展示了这种差异 使用async / await,调用线程不会阻塞并继续执行。static void Main(string[] args){&nbsp; &nbsp; WriteOutput("Program Begin");&nbsp; &nbsp; // DoAsTask();&nbsp; &nbsp; DoAsAsync();&nbsp; &nbsp; WriteOutput("Program End");&nbsp; &nbsp; Console.ReadLine();}static void DoAsTask(){&nbsp; &nbsp; WriteOutput("1 - Starting");&nbsp; &nbsp; var t = Task.Factory.StartNew<int>(DoSomethingThatTakesTime);&nbsp; &nbsp; WriteOutput("2 - Task started");&nbsp; &nbsp; t.Wait();&nbsp; &nbsp; WriteOutput("3 - Task completed with result: " + t.Result);}static async Task DoAsAsync(){&nbsp; &nbsp; WriteOutput("1 - Starting");&nbsp; &nbsp; var t = Task.Factory.StartNew<int>(DoSomethingThatTakesTime);&nbsp; &nbsp; WriteOutput("2 - Task started");&nbsp; &nbsp; var result = await t;&nbsp; &nbsp; WriteOutput("3 - Task completed with result: " + result);}static int DoSomethingThatTakesTime(){&nbsp; &nbsp; WriteOutput("A - Started something");&nbsp; &nbsp; Thread.Sleep(1000);&nbsp; &nbsp; WriteOutput("B - Completed something");&nbsp; &nbsp; return 123;}static void WriteOutput(string message){&nbsp; &nbsp; Console.WriteLine("[{0}] {1}", Thread.CurrentThread.ManagedThreadId, message);}DoAsTask输出:[1]计划开始[1] 1&nbsp; - 开始[1] 2&nbsp; - 任务开始[3] A&nbsp; - 开始了一些事情[3] B&nbsp; - 完成了一些事情[1] 3&nbsp; - 任务完成,结果:123[1]程序结束DoAsAsync输出:[1]计划开始[1] 1&nbsp; - 开始[1] 2&nbsp; - 任务开始[3] A&nbsp; - 开始了一些事情[1]程序结束[3] B&nbsp; - 完成了一些事情[3] 3&nbsp; - 完成任务结果:123更新:通过在输出中显示线程ID来改进示例。
打开App,查看更多内容
随时随地看视频慕课网APP