简体   繁体   English

异步等待任务 <T> 没有异步/等待

[英]Asynchronously wait for Task<T> without async/await

I want my Task to complete before I perform other actions. 在执行其他操作之前,我希望我的Task完成。 If I use 如果我使用

Task.WaitAll(task);

it certainly blocks my UI thread. 它肯定会阻塞我的UI线程。 What is the best way to wait asynchronously for a Task to complete, without using async / await (I'm using .NET v4.0)? 在不使用async / await情况下async / await Task完成的最佳方法是什么(我使用的是.NET v4.0)?

You can use async / await also in .NET 4.0 when you add the Microsoft.Bcl.Async NuGet package to your project. Microsoft.Bcl.Async NuGet package添加到项目中时,也可以在.NET 4.0中使用async / await Besides this, you have not much choice - either you use asynchronous calls or your block your thread with the method you mentioned already. 除此之外,您没有太多选择 - 要么使用异步调用,要么使用您提到的方法阻止您的线程。

However, you're not bound to await when using asynchronous method. 但是,在使用异步方法时,您不必await You can also use a callback: 您还可以使用回调:

task.ContinueWith(r => {
    /* code */
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM