根据我在C#中的所有异步/等待教程中的理解,我得到了当我们定义异步方法时,它意味着我们可以在其中添加await关键字并将任务作为参数,以使方法返回到调用者,如果任务太长了.然后,当任务完成时,该方法可以恢复.
public void MyMainMethod()
{
DoSomethingAsync();
KeepOnDoingOtherStuff();
}
public async Task DoSomethingAsync()
{
//Do whatever
await LongOperation();
// Rest of the method that is executed when the long operation is
//finished.
}
Run Code Online (Sandbox Code Playgroud)
因此,当长时间操作正在等待时,DoSomething async将手提供给MainMethod并执行KeepOnDoingOtherStuff.好
现在我的问题是我的DoSomethingAsync本身是否等待.在这种情况下:
public async void MyMainMethod()
{
await DoSomethingAsync();
//Here is a critical method that I want to execute only when
//DoSomethingAsync is finished
CriticalStuff();
}
public async Task DoSomethingAsync()
{
//Do whatever
await LongOperation();
// Rest of the method that is executed when the long operation …Run Code Online (Sandbox Code Playgroud)