使用异步无的await [英] Using async without await

查看:152
本文介绍了使用异步无的await的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作一个函数异步,所以我只需添加异步是这样的:

I'd like to make a function async, so I simply add async like this:

public async static void something(){
}

您可以看到,它的返回类型为无效。我只是想这一功能,必须异步调用不会阻塞,因为回报是无效所以没有等待是必要的。

You can see that its return-type is void. I just want this function to be called asynchronously without blocking, since return is void so no await is needed.

不过的Visual Studio 2012就不能编译这一点,它说,我想念的await

But Visual Studio 2012 just cannot compile this, it says that I miss await?

能否请您指教的样本,使一个函数异步不使用的await

Could you please advise a sample that makes a function async without using await.

推荐答案

我想,也许你​​误解是什么异步一样。该警告是完全正确的:如果你标记你的方法异步,但不要使用等待的任何地方,那么你的方法< STRONG>不会是异步。如果你调用它,所有的方法中的code将同步执行。

I think that maybe you misunderstand what async does. The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be asynchronous. If you call it, all the code inside the method will execute synchronously.

此外,你应该尽量避免使用异步无效方法,他们提出处理异常困难。

Also, you should try to avoid using async void methods, they make handling exceptions difficult.

这篇关于使用异步无的await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆