等待一个无效的异步方法 [英] Wait for a void async method
本文介绍了等待一个无效的异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何等待 void async
方法完成其工作?
How can I wait for a void async
method to finish its job?
例如,我有一个像下面这样的函数:
for example, I have a function like below:
async void LoadBlahBlah()
{
await blah();
...
}
现在我想在继续其他地方之前确保所有内容都已加载.
now I want to make sure that everything has been loaded before continuing somewhere else.
推荐答案
最好的做法是标记函数 async void
仅当它是fire and forget方法时,如果你想await on,你应该将其标记为 async Task
.
Best practice is to mark function async void
only if it is fire and forget method, if you want to await on, you should mark it as async Task
.
如果你仍然想等待,那么就这样包装它await Task.Run(() => blah())
In case if you still want to await, then wrap it like so await Task.Run(() => blah())
这篇关于等待一个无效的异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文