I am trying to implement a HttpWebRequest timeout for my WP7 app, as the user could make a request, and the request will never come back, leaving a ProgressBar I have on the screen.
I saw this MSDN page: msdn page
Which uses
ThreadPool.RegisterWaitForSingleObject (result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true);
I was able to add this code, and link up all the variables, but when I add it to my code, It gives a NotSupportedOperation when getting to the line:
allDone.WaitOne();
If i comment it out, it gives the same NotSupportedOperation at my next line,
return _result_object; (function is private object SendBeginRequest())
How can I add a timeout in WP7? This way does not seem to work. I would prefer not to use WebClient due to the UI thread issue.
In case you missed it, allDone is supposed to be a ManualResetEvent, and you can pass either an integer number of milliseconds or a TimeSpan as the amount of time to wait before continuing. For example:
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. 
         }
     }
Alternatively, you could use a third party library such as Hammock, which enable syou to do timeouts and retry attempts (among other things). Depending on your project, that might be more than you need, though :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With