C#异步方法返回void与Task的区别详解
作者:金庆 时间:2021-06-29 10:00:35
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 后缀。
总结
来源:https://blog.csdn.net/jq0123/article/details/114064557
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解java中保持compareTo和equals同步
FasfDFS整合Java实现文件上传下载功能实例详解
![](https://img.aspxhome.com/file/2023/8/70268_0s.png)
C#中lock用法详解
C#调用存储过程详解(带返回值、参数输入输出等)
java数据结构和算法中哈希表知识点详解
![](https://img.aspxhome.com/file/2023/3/57963_0s.png)
C#新手常犯的错误汇总
通过JDK源码学习InputStream详解
详解Java递归实现树形结构的两种方式
![](https://img.aspxhome.com/file/2023/7/84267_0s.png)
javaWeb项目部署到阿里云服务器步骤详解
![](https://img.aspxhome.com/file/2023/9/58909_0s.png)
Java实现双向循环链表
浅谈MyBatis通用Mapper实现原理
Android高级组件ImageSwitcher图像切换器使用方法详解
![](https://img.aspxhome.com/file/2023/8/108248_0s.jpg)
使用C#调用百度地图并实现坐标点的设置以及读取示例
![](https://img.aspxhome.com/file/2023/5/87015_0s.png)
spring cloud zuul修改请求url的方法
java 中使用匿名类直接new接口详解及实例代码
java StringBuilder类的详解及简单实例
C#Process的OutputDataReceived事件不触发问题及解决
Java 实现分布式服务的调用链跟踪
![](https://img.aspxhome.com/file/2023/4/60124_0s.png)