服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#异步方法返回void与Task的区别详解

C#异步方法返回void与Task的区别详解

2022-11-04 11:55金庆 C#

这篇文章主要给大家介绍了关于C#异步方法返回void与Task的区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

C#异步方法返回void和Task的区别

如果异步(async关键字)方法有返回值,返回类型为T时,返回类型必然是 Task<T>。

但是如果没有返回值,异步方法的返回类型有2种,一个是返回 Task, 一个是返回 void:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 会用绿色波浪线警告:

?
1
2
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 后就正常了:

?
1
await CountDownAsync(3);

如果调用者不是一个异步方法,因为只有在异步方法中才可以使用 await,

或者并不想在此等待,如想同时执行多个 CountDownAsync(),

就不能应用 await 来消除警告。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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() 实现:

?
1
2
3
4
async void CountDown(int count)
{
 await CountDownAsync(count);
}

使用下划线变量忽略异步方法的返回值也可以消除警告:

?
1
2
3
4
5
6
7
void Test()
{
 ...
 _ = CountDownAsync(3);
 _ = CountDownAsync(3);
 ...
}

但是这样同时也会忽略 CountDownAsync() 中的异常。如以下异常会被忽略。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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() 接口:

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

可以这样调用并等待:

?
1
var resp = await client.GetFeatureAsync(req);

虽然返回类型不是Task<>, 但是可等待,所以添加了 Async 后缀。

总结

到此这篇关于C#异步方法返回void与Task区别的文章就介绍到这了,更多相关C#异步方法返回区别内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/jq0123/article/details/114064557

延伸 · 阅读

精彩推荐
  • C#C#程序员统计自己的代码行数

    C#程序员统计自己的代码行数

    这篇文章给大家讲解了下作为程序员如何统计自己写过的代码的行数,这个也是证明自己程序员能力的一个表现,一起来看下。...

    昆明--菜鸟入门9272022-02-15
  • C#C# 动态加载程序集信息

    C# 动态加载程序集信息

    在设计模式的策略模式中,需要动态加载程序集信息,本文通过一个简单的实例,来讲解动态加载Dll需要的知识点。下面跟着小编一起来看下吧...

    飞翔的月亮10072021-12-28
  • C#基于C#对用户密码使用MD5加密与解密

    基于C#对用户密码使用MD5加密与解密

    C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式。本文总结了通用的算法并结合了自己的一点小经验,分享给大家...

    Healer00711122021-11-05
  • C#C#中Hash table的一些操作方法讲解

    C#中Hash table的一些操作方法讲解

    今天小编就为大家分享一篇关于C#中Hash table的一些操作方法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小...

    chenqiangdage8612022-03-08
  • C#Unity实现VR中在黑板上写字效果

    Unity实现VR中在黑板上写字效果

    这篇文章主要为大家详细介绍了Unity实现VR中在黑板上写字效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Marsir7942022-07-28
  • C#解析C#设计模式之单例模式

    解析C#设计模式之单例模式

    这篇文章主要介绍了C#设计模式之单例模式的相关资料,帮助大家更好的理解和学习c# 设计模式的内容,感兴趣的朋友可以了解下...

    yangyang10632022-10-19
  • C#C#实现字符串首字母大写的方法示例

    C#实现字符串首字母大写的方法示例

    这篇文章主要给大家介绍了关于利用C#实现字符串首字母大写的相关资料,这是在最近工作中遇到的一个需求,文中通过示例代码介绍的非常详细,对大家...

    lindexi5232022-02-19
  • C#C#实现推送钉钉消息的方法示例

    C#实现推送钉钉消息的方法示例

    这篇文章主要介绍了C#实现推送钉钉消息的方法,结合实例形式分析了C#使用钉钉API实现消息推送的相关操作技巧与注意事项,需要的朋友可以参考下...

    啊ten7532022-03-09