如何等待响应并对其进行操作?
我有一个新问题。我想对响应进行一些操作,但我得到一个 NullReferenceException,因为它尚未到达...这是我的代码:
public partial class MainPage : PhoneApplicationPage
{
public static string res = null;
// Constructor
public MainPage()
{
InitializeComponent();
string Url = "http://twitter.com";
WebRequest req = WebRequest.Create(Url);
req.BeginGetResponse(new AsyncCallback(request_CallBack), req);
int i = MainPage.res.Length; // NullReferenceException
}
void request_CallBack(IAsyncResult result)
{
WebRequest webRequest = result.AsyncState as WebRequest;
WebResponse response = (WebResponse)webRequest.EndGetResponse(result);
Stream baseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(baseStream))
{
res = reader.ReadToEnd();
Dispatcher.BeginInvoke(() => { MessageBox.Show("The response is arrived."); });
Dispatcher.BeginInvoke(() => { tbResponse.Text = res; });
}
}
}
但是当我使用 ManualResetEvent 类时,我的应用程序只是挂起,因为 if(dataReady.WaitOne()) 行。以下是 ManualResetEvent 类的完整代码:
public partial class MainPage : PhoneApplicationPage
{
public static string res = null;
ManualResetEvent dataReady;
// Constructor
public MainPage()
{
InitializeComponent();
string Url = "http://twitter.com";
dataReady = new ManualResetEvent(false);
WebRequest req = WebRequest.Create(Url);
req.BeginGetResponse(new AsyncCallback(request_CallBack), req);
if (dataReady.WaitOne())
{
int i = MainPage.res.Length;
}
}
void request_CallBack(IAsyncResult result)
{
WebRequest webRequest = result.AsyncState as WebRequest;
WebResponse response = (WebResponse)webRequest.EndGetResponse(result);
Stream baseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(baseStream))
{
res = reader.ReadToEnd();
Dispatcher.BeginInvoke(() => { MessageBox.Show("The response is arrived."); });
Dispatcher.BeginInvoke(() => { tbResponse.Text = res; });
}
dataReady.Set();
}
}
那么,我的问题是:如何等待响应并对其进行操作? (我尝试使用Application.DoEvent方法,但它在WP7中不存在......)
I have one new problem. I want to do some operations with the response, but I get a NullReferenceException, because it isn't arrived yet... Here is my code:
public partial class MainPage : PhoneApplicationPage
{
public static string res = null;
// Constructor
public MainPage()
{
InitializeComponent();
string Url = "http://twitter.com";
WebRequest req = WebRequest.Create(Url);
req.BeginGetResponse(new AsyncCallback(request_CallBack), req);
int i = MainPage.res.Length; // NullReferenceException
}
void request_CallBack(IAsyncResult result)
{
WebRequest webRequest = result.AsyncState as WebRequest;
WebResponse response = (WebResponse)webRequest.EndGetResponse(result);
Stream baseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(baseStream))
{
res = reader.ReadToEnd();
Dispatcher.BeginInvoke(() => { MessageBox.Show("The response is arrived."); });
Dispatcher.BeginInvoke(() => { tbResponse.Text = res; });
}
}
}
But when I use the ManualResetEvent class, my app is just hanging, because of the
if(dataReady.WaitOne()) line. Here is the complete code with the ManualResetEvent class:
public partial class MainPage : PhoneApplicationPage
{
public static string res = null;
ManualResetEvent dataReady;
// Constructor
public MainPage()
{
InitializeComponent();
string Url = "http://twitter.com";
dataReady = new ManualResetEvent(false);
WebRequest req = WebRequest.Create(Url);
req.BeginGetResponse(new AsyncCallback(request_CallBack), req);
if (dataReady.WaitOne())
{
int i = MainPage.res.Length;
}
}
void request_CallBack(IAsyncResult result)
{
WebRequest webRequest = result.AsyncState as WebRequest;
WebResponse response = (WebResponse)webRequest.EndGetResponse(result);
Stream baseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(baseStream))
{
res = reader.ReadToEnd();
Dispatcher.BeginInvoke(() => { MessageBox.Show("The response is arrived."); });
Dispatcher.BeginInvoke(() => { tbResponse.Text = res; });
}
dataReady.Set();
}
}
So, there is my question: How can I wait the response and do operations with it?
(I tried to use the Application.DoEvent method, but it isn't exist in WP7...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能将该代码放在
request_CallBack
函数中吗?喜欢
cannot you put that code
on
request_CallBack
function ?like
当前您正在使用
WebRequest
的异步方法之一。如果您想等待响应,只需使用等效的同步方法GetResponse
即可。http://msdn.microsoft.com/en-us /library/system.net.webrequest.getresponse.aspx
Currenlty you are using one of the asynchronous methods of
WebRequest
. If you want to wait for a response, you can simply use the equivalent synchronous methodGetResponse
.http://msdn.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx
{
公共静态字符串res = null;
}
{
public static string res = null;
}