C#異步方法返回void與Task的區(qū)別詳解
C#異步方法返回void和Task的區(qū)別
如果異步(async關(guān)鍵字)方法有返回值,返回類(lèi)型為T(mén)時(shí),返回類(lèi)型必然是 Task<T>。
但是如果沒(méi)有返回值,異步方法的返回類(lèi)型有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)前方法。請(qǐ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 來(lái)消除警告。
此時(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.
對(duì) Async 后綴的說(shuō)明
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 生成的代碼中,異步請(qǐng)求返回了一個(gè) AsyncCall 對(duì)象,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);
雖然返回類(lèi)型不是Task<>, 但是可等待,所以添加了 Async 后綴。
總結(jié)
到此這篇關(guān)于C#異步方法返回void與Task區(qū)別的文章就介紹到這了,更多相關(guān)C#異步方法返回區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用iTextSharp設(shè)置PDF所有頁(yè)面背景圖功能實(shí)例
這篇文章主要介紹了C#使用iTextSharp設(shè)置PDF所有頁(yè)面背景圖功能,實(shí)例分析了C#使用iTextSharp設(shè)置PDF頁(yè)面背景圖的實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
C#基礎(chǔ)知識(shí)系列八const和readonly關(guān)鍵字詳細(xì)介紹
這篇文章主要介紹了C#中的const和readonly關(guān)鍵字,有需要的朋友可以參考一下2014-01-01
C#實(shí)現(xiàn)高效查找替換Excel表格數(shù)據(jù)或文本
在現(xiàn)代數(shù)據(jù)驅(qū)動(dòng)的業(yè)務(wù)環(huán)境中,Excel表格扮演著不可或缺的角色,本文將深入探討如何利用C#編程語(yǔ)言,精準(zhǔn)地實(shí)現(xiàn)Excel表格中的數(shù)據(jù)和文本查找與替換,感興趣的小伙伴可以了解下2025-09-09
詳解C# parallel中并行計(jì)算的四種寫(xiě)法總結(jié)
在C#中,parallel關(guān)鍵字可以用于并行計(jì)算。本文為大家總結(jié)了四種C# parallel并行計(jì)算的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-11-11

