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!
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!