HttpWebRequest hangs the application

S

sam

Hello,
When I send some HttpWebRequest while I'm out of the wireless area,
the request will prevent me from exiting out of the application right
away. Does anyone have any idea what the problem might be? I'd really
appreciate your help.

Here's my little app.:

public class Form1:System.Windows.Forms.Form
{
...

private void button1_Click(object sender, System.EventArgs e)
{
bool done=false;
try
{
HttpWebRequest request=
(HttpWebRequest)WebRequest.Create("http://myMachine/virtualDir/ping.htm");
request.Timeout=2000;
HttpWebResponse resp=(HttpWebResponse)request.GetResponse();
done=true;
resp.Close();
}
catch
{
}
if (done)
label1.Text+=" 1 ";
else
label1.Text+=" 0 ";
}
...
private void button2_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
...
}

The workflow would be: click button1 to get the "0" on the label and
then click button2 right away to exit out of the application. It'll
wait for about 35sec. before it can actually exit.

What I want to achieve here is simply timeout the request if it takes
more than some seconds. I tried spawning another thread to do the
HttpWebRequest and have the main thread check the result for only some
seconds (still leave the worker thread to finish to clean up things);
I also tried using HttpWebRequest.BeginGetResponse() and
HttpWebRequest.Abort() to implement this. And the outcome is the same,
except sometimes it'll throw ObjectDisposeException (even though I
tried to ignore the errors as shown below).

private void button1_Click(object sender, System.EventArgs e)
{
bool done=false;
try
{
HttpWebRequest myHttpWebRequest=
(HttpWebRequest)WebRequest.Create(textBox1.Text);
IAsyncResult asyncResult =
myHttpWebRequest.BeginGetResponse(null,null);

for (int i=0;i<2*2;i++) //timeout in 2 sec.
{
if (asyncResult.IsCompleted)
{
HttpWebResponse response =(HttpWebResponse)
myHttpWebRequest.EndGetResponse(asyncResult);
response.Close();
done=true;
break;
}
Thread.Sleep(500);
}

myHttpWebRequest.Abort();
}
catch
{
}

if (done)
label1.Text+=" 1 ";
else
label1.Text+=" 0 ";
}


Thanks a lot!
 
D

David Kline [msft]

You do not want to use the HttpWebRequest.Timeout property to timeout the
request. What you are better off doing is to make an asynchronous web
request and aborting the request if your specified time expires (typically
determined using a Timer object)

Thanks,
David Kline
Microsoft .NET Compact Framework
--------------------------------
This posting is provided “AS IS” with no warranties, and confers no
rights.

Please do not send email directly to this alias. This alias is for
newsgroup purposes only. To correspond with me directly, remove the
'online' from
my alias.
 
S

sam

You do not want to use the HttpWebRequest.Timeout property to timeout the
request. What you are better off doing is to make an asynchronous web
request and aborting the request if your specified time expires (typically
determined using a Timer object)

Thanks,
David Kline
Microsoft .NET Compact Framework

yeah, we did try send the http web request in the async manner (with
timer as well), but HttpWebRequest.Abort() doesn't seem to really get
rid of the call in the background. After the abort() function is
called, the application is able to continue on, but if you choose to
Exit() right away, the application will hang there for about half a
minute if your last webrequest is out of the wireless coverage.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top