Wil*_*ani 5 c# windows-phone-7
我正在尝试为我的WP7应用程序实现HttpWebRequest超时,因为用户可以发出请求,并且请求永远不会回来,留下我在屏幕上的ProgressBar.
我看到了这个MSDN页面:msdn page
哪个用途
ThreadPool.RegisterWaitForSingleObject (result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true);
Run Code Online (Sandbox Code Playgroud)
我能够添加这个代码,并链接所有变量,但当我将它添加到我的代码时,它给出了一个NotSupportedOperation到达行的时间:
allDone.WaitOne();
Run Code Online (Sandbox Code Playgroud)
如果我发表评论,它会NotSupportedOperation在我的下一行给出相同的信息,
return _result_object;(功能是private object SendBeginRequest())
如何在WP7中添加超时?这种方式似乎不起作用.由于UI线程问题,我宁愿不使用WebClient.
如果您错过了它,allDone应该是a ManualResetEvent,并且您可以传递整数毫秒或TimeSpan作为继续之前等待的时间量.例如:
private ManualResetEvent _waitHandle = new ManualResetEvent(false);
private bool _timedOut;
...
this._timedOut = false;
this._waitHandle.Reset();
HttpWebRequest request = HttpWebRequest.CreateHttp("http://cloudstore.blogspot.com");
request.BeginGetResponse(this.GetResponse_Complete, request);
bool signalled = this._waitHandle.WaitOne(5);
if (false == signalled)
{
// Handle the timed out scenario.
this._timedOut = true;
}
private void GetResponse_Complete(IAsyncResult result)
{
// Process the response if we didn't time out.
if (false == this._timedOut)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
WebResponse response = request.EndGetResponse(result);
// Handle response.
}
}Run Code Online (Sandbox Code Playgroud)
或者,您可以使用第三方库,例如Hammock,它可以使syou执行超时和重试尝试(以及其他内容).根据您的项目,这可能超出您的需要,但:)