是否在不使用await的情况下调用异步方法中的await仍然异步? [英] Are awaits in async method called without await still asynchronous?

查看:51
本文介绍了是否在不使用await的情况下调用异步方法中的await仍然异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

public static void Run() {
    DoStuffAsync();
}

public static async Task DoStuffAsync() {
     PerformCalc();
     await DoLongTaskAsync();
     PerformAnotherCalc();
}

比方说,我叫 Run().我对行为有一些疑问:

Let's say I call Run(). I have a few questions regarding behaviour:

  1. 是否会在与调用 Run()的线程相同的线程上同步调用 PerformCalc()?
  2. 会异步还是同步调用 DoLongTaskAsync()?换句话说,是否可以在 DoLongTaskAsync()完成之前调用 PerformAnotherCalc()?
  3. 随后, DoStuffAsync()方法能否在完成 DoLongAsyncTask()执行之前返回?
  1. Will PerformCalc() be called synchronously on the same thread as the one that called Run()?
  2. Will DoLongTaskAsync() be called asynchronously or synchronously? In other words, will/can PerformAnotherCalc() be called before DoLongTaskAsync() has finished?
  3. Subsequently, can the DoStuffAsync() method return before execution of DoLongAsyncTask() has completed?

推荐答案

异步方法始终 start 同步运行.魔术发生在 await 上,但是仅当 await 被赋予尚未完成的 Task 任务时.

Async methods always start running synchronously. The magic happens at await, but only when await is given a Task that has not completed.

在您的示例中,这就是您调用 Run()时将发生的情况:

In your example, this is what will happen when you call Run():

  1. 跳转到 DoStuffAsync()
  2. 跳转到 PerformCalc()
  3. 跳转到 DoLongTaskAsync()
  4. 如果 DoLongTaskAsync()是真正的异步操作,并且返回不完整的 Task ,则 await 会执行job和 DoStuffAsync()返回不完整的 Task Run().
  5. 由于未等待任务,因此 Run()完成.
  6. DoLongTaskAsync()完成时, DoStuffAsync()恢复,并跳转到 PerformAnotherCalc().
  1. Jump to DoStuffAsync()
  2. Jump to PerformCalc()
  3. Jump to DoLongTaskAsync()
  4. If DoLongTaskAsync() is a truly asynchronous operation and returns an incomplete Task, then await does its job and DoStuffAsync() returns an incomplete Task to Run().
  5. Since the task is not awaited, Run() completes.
  6. When DoLongTaskAsync() completes, DoStuffAsync() resumes, and jumps to PerformAnotherCalc().

所有这些可以发生在同一线程上.

All of that can happen on the same thread.

所以回答您的问题:

  1. 是的.如果它是 async 方法,则它可能最终会消失并在其他线程上运行.但是它将在同一线程上同步启动.
  2. DoLongTaskAsync()将被异步调用,但是在 DoLongTaskAsync()完成之前不会调用 PerformAnotherCalc(),因为您使用了等待.
  3. 是的.这就是 await 的工作方式.它将返回不完整的 Task (即,仅当 DoLongTaskAsync()是真正异步的并且返回不完整的 Task 时).然后,一旦 DoLongTaskAsync()完成后, DoStuffAsync()的执行就会从中断的地方恢复.
  1. Yes. If it is an async method, it might end up going out and doing things on other threads. But it will start synchronously on the same thread.
  2. DoLongTaskAsync() will be called asynchronously, but PerformAnotherCalc() will not be called before DoLongTaskAsync() finishes, because you used await.
  3. Yes. This is how await works. It will return an incomplete Task (that is, only if DoLongTaskAsync() is truly asynchronous and returns an incomplete Task). Then once DoLongTaskAsync() finishes, execution of DoStuffAsync() resumes where it left off.

这篇关于是否在不使用await的情况下调用异步方法中的await仍然异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆