如何等待异步void完成? [英] How to wait for async void to complete?

查看:134
本文介绍了如何等待异步void完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要等待我的async void完成.它具有一些await Task.Delay函数,在继续之前我需要它们完成.这个void不会返回任何内容,因此我看不到将其设为async Task<>的理由,而且我还在此void内部改组了数组,并声明了一些变量.那么我该怎么做呢?是async Task<>唯一的方法吗?我的空白不希望任何参数/输入.

i need to wait for my async void to finish. It has some await Task.Delay functions in it and I need them to finish before it continues. This void doesn't return anything so I don't see a reason to make it async Task<> and I also shuffle an array inside this void and I declare some variables. So how can I do this ? Is async Task<> the only way ? My void is not expecting any parameters/inputs.

推荐答案

返回

Return Task (not Task<T>) instead of void. Note, the non-generic form of Task does not need to wrap a value. It's just for signalling completion or errors, not results. In the case of async methods, this is equivalent to a synchronous void return type. It means you can wait for your method to complete.

这篇关于如何等待异步void完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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