本文介绍了等待的最后一个方法行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍在学习异步计谋。我碰到的例子类似以下内容:

Still learning about async-await. I bumped into examples similar to following:

public async Task MethodAsync()
{
  await Method01Async();
  await Method02Async();
}

什么最后的await的目的是什么? Method02Async是MethodAsync方法的最后一行。所以没有任何方法剩下的 - 没有任何低于行 - 没有什么被称为由编译器生成的回调......我缺少什么

What is the purpose of the last await? Method02Async is the last line of MethodAsync method. So there is no any method remainder - no any lines below - no anything to be called in the callback generated by the compiler... Am I missing anything?

推荐答案

有实际上就是法余数 - 它完成了工作按 MethodAsync 。

There actually is a "method remainder" - it completes the Task returned by MethodAsync.

(返回值) Method02Async 的等待让 MethodAsync 才能完成 Method02Async 完成。

(The return value of) Method02Async is awaited so that MethodAsync is not completed until Method02Async completes.

如果你有:

public async Task MethodAsync()
{
  await Method01Async();
  Method02Async();
}

然后 MethodAsync 将(异步)等 Method01Async 来完成,然后启动 Method02Async MethodAsync 之后会完成,而 Method02Async 可能仍在进行中。

Then the MethodAsync will (asynchronously) wait for Method01Async to complete and then start Method02Async. MethodAsync will then complete while Method02Async may still be in progress.

您的方式:

public async Task MethodAsync()
{
  await Method01Async();
  await Method02Async();
}

意思是 MethodAsync 将(异步)等 Method01Async 来完成,然后(异步)等 Method02Async 来完成,只有这样, MethodAsync 完成。

Means that MethodAsync will (asynchronously) wait for Method01Async to complete and then (asynchronously) wait for Method02Async to complete, and only then will MethodAsync complete.

这篇关于等待的最后一个方法行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 14:44