如何等待异步void完成? [英] How to wait for async void to complete?
问题描述
我需要等待我的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.
推荐答案
返回 Task<T>
),而不是void
.注意,Task
的非泛型形式不需要包装值.它仅用于表示完成或错误,而不是结果.对于async
方法,这等效于同步void
返回类型.这意味着您可以等待方法完成.
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屋!