HttpWebRequest timeout in WP7

I am trying to implement the HttpWebRequest timeout for my WP7 application, as the user can make a request and the request will never return, leaving a ProgressBar on the screen.

I saw this MSDN page: msdn page

What uses

ThreadPool.RegisterWaitForSingleObject (result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true);

I managed to add this code and bind all the variables, but when I add it to my code, it gives NotSupportedOperationwhen going to the line:

allDone.WaitOne();

If I comment on this, it gives the same NotSupportedOperationon my next line,

return _result_object;(function private object SendBeginRequest())

How to add timeout in WP7? This method does not seem to work. I would prefer not to use WebClient because of a problem with the UI thread.

+5
1

, allDone 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. 
         }
     }

, ​​ Hammock, syou - ( ). , :)

+6

All Articles