WP7上带有HttpWebRequest的ManualResetEvent [英] ManualResetEvent with HttpWebRequest on WP7
问题描述
首先,可以将其标记为以下线程的重复项:等待HttpWebRequest.BeginGetResponse在Windows Phone 7中完成,但是,该线程中的响应并没有帮助我解决问题.
To start off with, this might be tagged as a duplicate of the following thread: Wait for HttpWebRequest.BeginGetResponse to finish in Windows Phone 7, however, the responses in that thread did not help me get over my problem.
首先,我在UI线程上收集用户数据以处理应用程序注册,在这里我还有一个ManualResetEvent实例:
To begin with, I am collecting user data on the UI Thread in order to process application registration, where I also have an instance of ManualResetEvent:
private static ManualResetEvent registrationEvent = new ManualResetEvent(false);
我有另一个线程来处理注册过程(并包括HttpWebRequest.BeginGetResponse()及其对应的回调方法.)
I have another thread which handles the registration process (and includes the HttpWebRequest.BeginGetResponse() and its corresponding callback method.)
Thread t = new Thread(() => RegistrationHandler.sendRegistrationData(url));
t.Start();
此调用之后,我立即调用
Right after this call, I block the current (UI) thread with a call to
registrationEvent.WaitOne();
//Process the response, update some UI elements and navigate to a different page.
httpSessionCompleted(response);
一旦处理注册过程的线程开始,我将实例化HttpWebRequest并在其上调用BeginGetResponse()方法.
Once the thread handling the registration process starts, I am instantiating HttpWebRequest and invoking the BeginGetResponse() method on it.
try
{
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.Method = "POST";
request.ContentType = mimeType;
request.BeginGetResponse(new AsyncCallback(GetRequestCallback), request);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in sendData(): {0}", ex.Message);
}
现在的问题是,永远不会调用回调方法(下面的代码),并且应用程序只会冻结.似乎也没有引发任何异常.
Now the issue is that the callback method (code below) is never invoked, and the application just freezes. There also doesn't seem to be any exception(s) thrown either.
try
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
if (request != null)
{
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
String result = reader.ReadToEnd();
Globals.HostResponse = result;
//Signalling the calling thread to continue execution
RegistrationPage.RegistrationEvent.Set();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in GetRequestCallback(): {0}", ex.Message);
}
理想情况下,我希望我的应用程序在回调方法完成执行后从httpSessionCompleted()继续.有人可以帮我一些指导/建议吗?
I ideally want my application to continue from httpSessionCompleted() after the callback method finishes execution. Can someone please help me with some guidance/suggestions?
很抱歉,很冗长.谢谢!
Sorry for being verbose. Thanks!
推荐答案
您不应阻止 UI线程
,而应使用回调模式.查看以下内容: Windows Phone 7-等待Webclient完成.希望这会有所帮助
You should not block UI thread
, use callback pattern instead. Look at this: Windows Phone 7 - wait for Webclient to complete . Hope this helps
这篇关于WP7上带有HttpWebRequest的ManualResetEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!