等待HttpWebRequest.BeginGetResponse在Windows Phone 7的完成 [英] Wait for HttpWebRequest.BeginGetResponse to finish in Windows Phone 7

查看:155
本文介绍了等待HttpWebRequest.BeginGetResponse在Windows Phone 7的完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用在Silverlight的异步的HttpWebRequest Windows Phone的。直到我到了那里我应该叫

I'm trying to use the async HttpWebRequest in Silverlight for Windows Phone. All works perfect until I get to the where I should call

private static ManualResetEvent allDone = new ManualResetEvent(false);
...
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
allDone.WaitOne();
Debug.WriteLine("All done!");

GetResponseCallback

private void GetResponseCallback(IAsyncResult asynchronousResult)
{
    try
    {
        request = (HttpWebRequest)asynchronousResult.AsyncState;
        response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        allDone.Set();
    }
    catch (Exception e)
    {
        Debug.WriteLine("Got Exception in GetResponseCallback: " + e.Message);
    }
}



调用后 allDone .WaitOne(); 它只是挂起...

为什么

推荐答案

这只是需要多一点的转变在从阻塞/等待在WP7平台上的异步角度思考思考了。其结果是,用户总是能够与UI交互。

This just takes a bit of a shift in thinking away from blocking/waiting to thinking in async terms on the WP7 platform. The result is the user is always able to interact with the UI.

移动呼叫到您的完成码(在这种情况下的WriteLine)插入CompletedEventHandler和任何UI更新马歇尔回到UI线程以

Move a call to your completion code (writeline in this case) into your CompletedEventHandler and for any UI updates marshall back to the UI thread with

Dispatcher.BeginInvoke( () => { /* your UI update code */ } )

如果有一些不应该,而你的异步操作执行时那么这些与可交互的UI元素控件可以隐藏或禁用的临时

If there are any UI elements that should not be interacted with while your async op is executing then these controls can be hidden or disabled for the interim.

这篇关于等待HttpWebRequest.BeginGetResponse在Windows Phone 7的完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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