C#異步方法返回void與Task的區別詳解

C#異步方法返回void和Task的區別

如果異步(async關鍵字)方法有返回值,返回類型為T時,返回類型必然是 Task<T>。

但是如果沒有返回值,異步方法的返回類型有2種,一個是返回 Task, 一個是返回 void:

 public async Task CountDownAsync(int count)
 {
  for (int i = count; i >= 0; i--)
  {
   await Task.Delay(1000); 
  }
 }

 public async void CountDown(int count)
 {
  for (int i = count; i >= 0; i--)
  {
   await Task.Delay(1000);
  }
 }

調用時,如果返回 Task, 但返回值被忽略時,VS 會用綠色波浪線警告:

 CountDownAsync(3);
 ~~~~~~~~~~~~~~~~~

信息為:

(awaitable) Task AsyncExample.CountDownAsync(int count)

Usage:
 await CountDownAsync(…);

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the ‘await’ operator to the result of the call.

中文為:

CS4014:由於此調用不會等待,因此在此調用完成之前將會繼續執行當前方法。請考慮將”await”運算符應用於調用結果。

添加 await 後就正常瞭:

 await CountDownAsync(3);

如果調用者不是一個異步方法,因為隻有在異步方法中才可以使用 await,

或者並不想在此等待,如想同時執行多個 CountDownAsync(),

就不能應用 await 來消除警告。

此時可以改用 void 返回值的版本:

void Test()
{
 ...
 CountDown(3);
 CountDown(3);
 ...
}

async void CountDown(int count)
{
 for (int i = count; i >= 0; i--)
 {
  await Task.Delay(1000);
 }
}

Never call async Task methods without also awaiting on the returned Task. If you don’t want to wait for the async behaviour to complete, you should call an async void method instead.

摘自:http://www.stevevermeulen.com/index.php/2017/09/using-async-await-in-unity3d-2017/

CountDown() 可以直接調用 CountDownAsync() 實現:

async void CountDown(int count)
{
 await CountDownAsync(count);
}

使用下劃線變量忽略異步方法的返回值也可以消除警告:

void Test()
{
 ...
 _ = CountDownAsync(3);
 _ = CountDownAsync(3);
 ...
}

但是這樣同時也會忽略 CountDownAsync() 中的異常。如以下異常會被忽略。

void Test()
{
 ...
 _ = CountDownAsync(3);
 ...
}

async Task CountDownAsync(int count)
{
 for (int i = count; i >= 0; i--)
 {
  await Task.Delay(1000); 
 }
 throw new Exception();
}

如果是調用返回 void 的異步方法,Unity 會報錯:

Exception: Exception of type ‘System.Exception’ was thrown.

對 Async 後綴的說明

You could say that the Async suffix convention is to communicate to the API user that the method is awaitable. For a method to be awaitable, it must return Task for a void, or Task<T> for a value-returning method, which means only the latter can be suffixed with Async.

摘自:https://stackoverflow.com/questions/15951774

grpc 生成的代碼中,異步請求返回瞭一個 AsyncCall 對象,AsyncCall 實現瞭 GetAwaiter() 接口:

  public virtual grpc::AsyncUnaryCall<global::Routeguide.Feature> GetFeatureAsync(global::Routeguide.Point request, ...)

可以這樣調用並等待:

 var resp = await client.GetFeatureAsync(req);

雖然返回類型不是Task<>, 但是可等待,所以添加瞭 Async 後綴。

總結

到此這篇關於C#異步方法返回void與Task區別的文章就介紹到這瞭,更多相關C#異步方法返回區別內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: