资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#中异步方法返回的void和Task有什么区别-创新互联

C#中异步方法返回的void和Task有什么区别?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为灌阳企业提供专业的网站制作、网站建设灌阳网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

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

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

但是如果没有返回值,异步方法的返回类型有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 for a value-returning method, which means only the latter can be suffixed with Async.

grpc 生成的代码中,异步请求返回了一个 AsyncCall 对象,AsyncCall 实现了 GetAwaiter() 接口:


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

可以这样调用并等待:

 var resp = await client.GetFeatureAsync(req);

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。


当前标题:C#中异步方法返回的void和Task有什么区别-创新互联
地址分享:http://cdkjz.cn/article/dcojgc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220