DownloadStringTaskAsync on WP7 hangs when retrieving Result

戏子无情 提交于 2020-02-07 03:13:45

问题


I converted a bunch of WP7 code to use DownloadStringTaskAsync instead of DownloadStringAsync using the Async CTP SP1. 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. Its works as I expect and I get a string with the contents of 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.

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.


回答1:


Windows Phone brings back HTTP requests on the UI thread. By accessing Result, you are blocking the UI thread, thus making it impossible for the response to come back.

Considering you are using the async CTP, why would you want to block at all?

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


来源:https://stackoverflow.com/questions/6448819/downloadstringtaskasync-on-wp7-hangs-when-retrieving-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!