简体   繁体   English

在HttpWebRequest中使用TPL任务

[英]Using TPL Tasks with HttpWebRequest

I was hoping to use the System.Threading.Task library for my asynchronous web requests in my WP7 project. 我希望在我的WP7项目中使用System.Threading.Task库来处理我的异步Web请求。 However, in WP7 (I believe) you have to use the HttpWebRequest class for http requests (something my app does a lot of). 但是,在WP7中(我相信)你必须使用HttpWebRequest类来处理http请求(我的应用程序做了很多事情)。 The HttpWebRequest class only has a BeginGetResponse method that forces you to use the old asynchronous programming model by requiring you to pass in a callback parameter. HttpWebRequest类只有一个BeginGetResponse方法,它强制您使用旧的异步编程模型,要求您传入一个回调参数。

Is there any way to enter modernity using WP7, or am I stuck with HttpWebRequests and callbacks? 有没有办法使用WP7进入现代性,还是我坚持使用HttpWebRequests和回调?

UPDATE Jon gave me some good direction on finding the right method to convert APM code to TPL using Task.Factory.FromAsync, but I am really struggling on using Task.Factory.FromAsync. 更新Jon给了我一些很好的指导,找到了使用Task.Factory.FromAsync将APM代码转换为TPL的正确方法,但我真的在努力使用Task.Factory.FromAsync。 The first parameter is an IAsyncResult, which I assume would be your BeginGetResponse or BeginGetRequest method. 第一个参数是IAsyncResult,我假设它是您的BeginGetResponse或BeginGetRequest方法。 But the second parameter, which the documentation I've read typically suggests should be your end method, is a Func. 但是第二个参数,我读过的文档通常建议应该是你的最终方法,是一个Func。 The EndGetRequest and EndGetResponse methods for HttpWebRequest return a Stream ... and at that point, I'm lost. HttpWebRequest的EndGetRequest和EndGetResponse方法返回一个Stream ......在那一点上,我迷路了。

In general, you can use TaskFactory.FromAsync to build a Task<T> from a begin/end-method pair. 通常,您可以使用TaskFactory.FromAsync从开始/结束方法对构建Task<T>

I don't know whether that's supported by the WP7 version of TPL, but it's where I'd start. 我不知道是否支持WP7版本的TPL,但它是我开始的地方。

See also: "TPL and Traditional .NET Framework Asynchronous Programming" . 另请参阅: “TPL和传统.NET Framework异步编程”

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

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