等待在.NET 4.0中的选择吗? [英] Await alternative in .NET 4.0?

查看:133
本文介绍了等待在.NET 4.0中的选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在.NET 4.0中的计谋关键字的最佳替代品?我有一个需要异步操作后,返回值的方法。我注意到的wait()方法阻塞的线程完全从而使异步操作也没用。我有哪些选择运行异步操作,同时还释放UI线程?

What would be the best alternative for the await keyword in .NET 4.0 ? I have a method which needs to return a value after an asynchronous operation. I noticed the wait() method blocks the thread completely thus rendering the asynchronous operation useless. What are my options to run the async operation while still freeing the UI thread ?

推荐答案

我觉得你的基本选项

  • 使用工作 .ContinueWith()
  • 使用异步 CTP 异步 / 等待
  • 使用无扩展
  • Using Task and .ContinueWith()
  • Using the Async CTP and async / await
  • Using Reactive Extensions

最简单的方法可能是安装异步CTP。据我所知道的许可证允许罗科梅使用。它修补了编译器,并配有150KB的DLL,您可以包括到您的项目。

The easiest way is probably to install the Async CTP. As far as I know the license allows comercial usage. It patches the compiler and comes with a 150kb dll that you can include into your project.

您可以使用工作 .ContinueWith()。但是,这意味着,你必须要与exeption处理一些努力和流量控制。

You can use Task and .ContinueWith(). But that means, that you have to take some effort with exeption handling and flow control.

任务的功能结构。这就是为什么 ContinueWith()不符合必要的结构,如拌匀循环或试戴搭上块。因此异步等待得到了介绍,使编译器可以帮助我们。

Tasks are a functional construct. That's why ContinueWith() does not mix well with imperative constructs like for loops or try-catch blocks. Therefore async and await got introduced, so that the compiler can help us out.

如果你不能有一个支持的编译器(即你使用.NET 4.0中),你最好的选择是使用的 TAP 连同一个功能框架。 无扩展是一个很好的框架,以处理异步方法。

If you can't have that support of the compiler (i.e. you use .Net 4.0), your best bet is to use the TAP together with a functional framework. Reactive Extensions is a very good framework to treat asynchronous methods.

只是谷歌的反应扩展任务开始。

Just google for "reactive extensions tasks" to get started.

这篇关于等待在.NET 4.0中的选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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