简体   繁体   English

检索结果时,WP7上的DownloadStringTaskAsync挂起

[英]DownloadStringTaskAsync on WP7 hangs when retrieving Result

I converted a bunch of WP7 code to use DownloadStringTaskAsync instead of DownloadStringAsync using the Async CTP SP1. 我使用Async CTP SP1将一堆WP7代码转换为使用DownloadStringTaskAsync而不是DownloadStringAsync。 It wasn't working so I boiled down my code a bunch and ended up with these 2 lines: 它没有工作,所以我把我的代码简化了一堆,结果是这两行:

var wc = new WebClient();
var result = wc.DownloadStringTaskAsync("http://www.weather.gov").Result;

If I run this method with a console app on my windows machine. 如果我在我的Windows机器上使用控制台应用程序运行此方法。 Its works as I expect and I get a string with the contents of weather.gov. 它按预期工作,我得到一个包含weather.gov内容的字符串。 If I run the same 2 lines in the constructor of App in a blank WP7 app, it hangs while waiting for Result to become available. 如果我在一个空白的WP7应用程序中在App的构造函数中运行相同的2行,它会在等待Result变为可用时挂起。

Can anyone help me fix these lines so they will work on the phone? 任何人都可以帮我修理这些线路,以便他们可以在手机上工作吗? Or is this a bug in the CTP and I should skip it for now. 或者这是CTP中的一个错误,我现在应该跳过它。

Windows Phone brings back HTTP requests on the UI thread. Windows Phone在UI线程上带回HTTP请求。 By accessing Result , you are blocking the UI thread, thus making it impossible for the response to come back. 通过访问Result ,您将阻止UI线程,从而使响应无法返回。

Considering you are using the async CTP, why would you want to block at all? 考虑到您使用的是异步CTP,为什么要阻止呢?

var result = await wc.DownloadStringTaskAsync("http://www.weather.gov");

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

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