C# 执行异步Web请求时出现问题
有人能告诉我这个简单的代码有什么错吗? 当我运行它时,它会挂起C# 执行异步Web请求时出现问题,c#,asynchronous,windows-phone-7,C#,Asynchronous,Windows Phone 7,有人能告诉我这个简单的代码有什么错吗? 当我运行它时,它会挂起 using (Stream postStream = request.EndGetRequestStream(asynchronousResult)) 如果我注释掉requestState.Wait.WaitOne();行代码正确执行,但显然不等待响应。我猜对EndGetRequestStream的调用以某种方式将我返回到主线程的上下文??我非常确定我的代码本质上与示例相同() 两件事: 我认为您不能重复使用相同的请求对象。i、
using (Stream postStream = request.EndGetRequestStream(asynchronousResult))
如果我注释掉requestState.Wait.WaitOne();行代码正确执行,但显然不等待响应。我猜对EndGetRequestStream的调用以某种方式将我返回到主线程的上下文??我非常确定我的代码本质上与示例相同()
两件事:
AsyncResult resultRequest = request.BeginGetRequestStream(null, null);
Stream postStream = request.EndGetRequestStream(asynchronousResult)
EndGetRequestStream将一直阻止,直到请求完成为止(如果您是从UI执行此操作,则这是错误的,但它仍然可以工作)。上述代码在Windows Phone 7上引发NullReferenceException。您最好这样做:var wait_handle=new ManualResetEvent(false);var result=request.BeginGetRequestStream((ar)=>wait_handle.Set(),null);等等;返回请求。EndGetRequestStream(结果);
AsyncResult resultRequest = request.BeginGetRequestStream(null, null);
Stream postStream = request.EndGetRequestStream(asynchronousResult)