Thanks Sam,
At least I know I have done all I can
Regards,
Éric
CF doesn't support aborting a thread and that's probably why the abort doesn't work here. Here's my testing program, I was able to get the control back after the request times out, but Request.Abort() didn't really do the job to clean up, at least the main application won't be able to exit right away (Application.Exit()). I had this problem several months ago and was hoping that .NET CF SP2 might solve the problem. Unfortunately.
public class AsyncHttpWebRequester
{
public static ResponsePackage SendRequest(string URL, int timeout)
{
ManualResetEvent manualEvent=new ManualResetEvent(false);
HttpWebRequest myHttpWebRequest= (HttpWebRequest)WebRequest.Create(URL);
myHttpWebRequest.Timeout=timeout;
ResponsePackage respPackage=new ResponsePackage(myHttpWebRequest, manualEvent);
IAsyncResult asyncResult = myHttpWebRequest.BeginGetResponse(new System.AsyncCallback(AsyncCalledBack),respPackage);
respPackage.EventSystem.WaitOne();
return respPackage;
}
public static void AsyncCalledBack(IAsyncResult asyncResult)
{
ResponsePackage respPackage=asyncResult.AsyncState as ResponsePackage;
HttpWebResponse response=null;
StreamReader strReader=null;
try
{
if (respPackage!=null && respPackage.Request!=null)
{
response=(HttpWebResponse)(respPackage.Request.EndGetResponse(asyncResult));
respPackage.StatusCode=response.StatusCode;
if (respPackage.StatusCode==HttpStatusCode.OK)
{
strReader=new System.IO.StreamReader(response.GetResponseStream());
respPackage.ResultString=strReader.ReadToEnd();
}
}
}
catch (Exception ex)
{
//Debug.Message("Exception happened: "+ex.Message);
respPackage.ResultString=ex.Message;
respPackage.Request.Abort();
}
finally
{
if (strReader!=null)
strReader.Close();
if (response!=null)
response.Close();
respPackage.EventSystem.Set();
}
}