且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Task.Run()和await Task.Run()有什么区别?

更新时间:2022-03-14 22:31:47

Task.Run()和等待Task.Run()有什么区别?

What is the difference between Task.Run() and await Task.Run()?

首先启动一个任务,然后在任务完成后在任务完成前立即进行工作.

The first starts a task and then does the work immediately after that task, before the task completes.

第二个任务开始一个任务,然后执行不同的操作,直到任务完成为止,这时它在任务完成后进行工作.

The second starts a task and then does different work until the task is completed, at which point it does the work after the task.

让我们做个比喻.您的第一个程序就是这样:

Let's make an analogy. Your first program is like doing this:

  • 雇人修剪草坪.
  • 告诉你的配偶修剪草坪.
  • 去观看Netflix.

您的第二个程序是:

  • 雇人修剪草坪.
  • 修剪草坪时观看Netflix.
  • 修剪完草坪并且电影结束后,告诉配偶修剪草坪.

很明显,这些是非常不同的工作流程.两者都是异步的,但是只有后者中有一个异步等待.我们异步等待告诉配偶,草坪被修剪了,直到草坪被修剪了.

Clearly those are very different workflows. Both are asynchronous, but only the latter has an asynchronous wait in it. We asynchronously wait to tell the spouse that the lawn is mowed until it actually is mowed.