小编Zep*_*lin的帖子

等待C#中的异步任务会发生什么?

根据我在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)

.net c# asynchronous

2
推荐指数
1
解决办法
236
查看次数

标签 统计

.net ×1

asynchronous ×1

c# ×1