溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#中異步方法返回的void和Task有什么區(qū)別

發(fā)布時(shí)間:2021-03-04 14:20:52 來源:億速云 閱讀:587 作者:Leah 欄目:開發(fā)技術(shù)

C#中異步方法返回的void和Task有什么區(qū)別?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

C#異步方法返回void和Task的區(qū)別

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

但是如果沒有返回值,異步方法的返回類型有2種,一個(gè)是返回 Task, 一個(gè)是返回 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);
  }
 }

調(diào)用時(shí),如果返回 Task, 但返回值被忽略時(shí),VS 會(huì)用綠色波浪線警告:

 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:由于此調(diào)用不會(huì)等待,因此在此調(diào)用完成之前將會(huì)繼續(xù)執(zhí)行當(dāng)前方法。請考慮將"await"運(yùn)算符應(yīng)用于調(diào)用結(jié)果。

添加 await 后就正常了:

 await CountDownAsync(3);

如果調(diào)用者不是一個(gè)異步方法,因?yàn)橹挥性诋惒椒椒ㄖ胁趴梢允褂?await,

或者并不想在此等待,如想同時(shí)執(zhí)行多個(gè) CountDownAsync(),

就不能應(yīng)用 await 來消除警告。

此時(shí)可以改用 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() 可以直接調(diào)用 CountDownAsync() 實(shí)現(xiàn):

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

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

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

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

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

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

如果是調(diào)用返回 void 的異步方法,Unity 會(huì)報(bào)錯(cuò):

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.

grpc 生成的代碼中,異步請求返回了一個(gè) AsyncCall 對象,AsyncCall 實(shí)現(xiàn)了 GetAwaiter() 接口:

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

可以這樣調(diào)用并等待:

 var resp = await client.GetFeatureAsync(req);

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI