在Windows Phone 8 Htt​​pWebRequest的同步 [英] HttpWebRequest synchronous on Windows Phone 8

查看:92
本文介绍了在Windows Phone 8 Htt​​pWebRequest的同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了设计/重用库可用既为WPF应用程序时,Windows Phone 8的应用程序和更高版本的Windows 8 / RT的应用程序,我们需要做出一些HttpWebRequest的来电,preferably同步不打破现有的应用程序。

In order to design / reuse a library usable both for WPF apps, Windows Phone 8 apps and later Windows 8 / RT apps, we need to make some HttpWebRequest call, preferably synchronously not to break existing apps.

问题是code已经到位使用ManuelResetEvent同步做网页的请求。它完美的WPF和Win 8 RT但在Windows Phone 8的(至少在仿真器),程序挂起这里说:

The problem is the code already in place use ManuelResetEvent to do web request synchronously. It works perfectly in WPF and Win 8 RT but in Windows Phone 8 (at least on emulator), the program hang as told here :

<一个href=\"http://stackoverflow.com/questions/11508258/manualresetevent-with-httpwebrequest-on-wp7\">ManualResetEvent与HttpWebRequest的WP7上

我都尝试封装在一个任务或线程调用,但它仍然是相同的。

I tried both to encapsulate the call in a Task or Thread but it is still the same.

所以,我改变了code使用异步/的await,当然这将是完美的WP8和放大器;运8,但我们的桌面应用程序是针对.NET 4.0框架XP的兼容性。当然,还有就是异步定位包的Visual Studio 2012,但有两个问题:

So, I changed the code to use async / await and it will of course be perfect for WP8 & Win 8 but our desktop app is targeting .NET 4.0 framework for XP compatibility. Sure, there is the Async Targeting Pack for Visual Studio 2012 but there is two problems with this :


  • 的桌面应用程序code应该重写/仔细异步/的await
  • 测试
  • 单声道的稳定版本不支持异步/等待(或许与异步靶向包在Windows上运行,然后在它运行时的单可以工作,编制?应测试)

于是,我试着拨打异步标记方法同步,正好看到。我曾经在这里给出的AsyncHelpers:

So, I tried to call the async tagged methods synchronously, just to see. I used the AsyncHelpers given here :

<一个href=\"http://stackoverflow.com/questions/5095183/how-would-i-run-an-async-taskt-method-synchronously\">How我将运行一个异步任务&LT; T&GT;方法同步?

它适用于WPF和Win 8应用程序,但它挂在Windows Phone 8。

It works on WPF and Win 8 apps, but it hangs on Windows Phone 8.

是否有人知道如何有一个同步code(或同步寻找code用的await /异步),可以随处使用,无需挂在Windows Phone?

Does someone know how to have a synchronous code (or synchronous looking code with await / async) that could be used everywhere without hanging on Windows Phone ?

推荐答案

在HttpWebRequest的使用UI线程处理请求,不要问我为什么。

The HttpWebRequest use UI thread to process a request, don't ask me why.

所以,如果你试图通过锁等待结果的UI线程,你会得到一个死锁条件,因为你锁定UI线程,直到请求是不完整的,而UI线程被锁定请求将是不完整的。

So if you try to wait a result by lock the UI thread, you will get a deadlock condition, because you lock UI thread until request is not complete and request will not be complete while UI thread is locked.

这篇关于在Windows Phone 8 Htt​​pWebRequest的同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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