Since WP7 HttpWebRequest does not support timeout, I'm using a timer to implement the functionality. Below is an example. I call GetConnection() from a UI form. But ReadCallback() is never executed till the timer time is over. Once the timer is stopped, then ReadCallBack() is triggered. Seems like the timer thread is blocking the HTTP response. Any help is appreciated. I've also tried ManualResetEvent and that has the same result too.
private HttpWebRequest conn;
private bool _timedOut = false;
private DispatcherTimer tmr;
public void GetConnection()
{
conn = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://www.contoso.com"));
conn.Method = "GET";
tmr = new DispatcherTimer();
tmr.Interval = TimeSpan.FromSeconds(10);
tmr.Tick += new EventHandler(tmr_Tick);
_stopTimer = false;
IAsyncResult resp = conn.BeginGetResponse(new AsyncCallback(ReadCallback), conn);
tmr.Start();
}
private void tmr_Tick(object sender, EventArgs e)
{
if (!_stopTimer)
{
tmr.Stop();
conn.Abort();
}
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
_stopTimer = true;
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
m_response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
}