Using async without await

痴心易碎 提交于 2019-11-26 11:20:29

问题


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.

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/12016567/using-async-without-await

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!