I have some a bit of code following :
On Form:
List<WebRequestUri> lwebrequest = new List<WebRequestUri>();
public Form1()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ParameterizedThreadStart((object context) =>
{
DoWork();
}));
thread.IsBackground = true;
thread.Start();
}
void DoWork()
{
lwebrequest = new List<WebRequestUri>();
for (int i = 0; i < 100; i++)
{
WebRequestUri wp = new WebRequestUri();
wp.Start();
lwebrequest.Add(wp);
}
}
private void Stop_Click(object sender, EventArgs e)
{
for (int i = 0; i < lwebrequest.Count; i++)
{
lwebrequest[i].Abort();
}
}
Worker Class:
class WebRequestUri
{
Thread thread = null;
WebRequest webRequest = null;
WebResponse webResponse = null;
StreamReader sr = null;
public void Start()
{
thread = new Thread(new ParameterizedThreadStart((object context) =>
{
SendRequest();
}));
thread.IsBackground = true;
thread.Start();
}
public void Abort()
{
if (webResponse != null) webResponse.Close();
if (webRequest != null) webRequest.Abort();
if (thread != null) thread.Abort();
}
public void SendRequest()
{
webRequest = WebRequest.Create("http://google.com");
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "GET";
try
{
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
catch (WebException)
{
}
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
webResponse = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
sr = new StreamReader(webResponse.GetResponseStream());
string response = sr.ReadToEnd();
Console.Write(response);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
I’m getting a problem that when i try to click stop button. I see my form is blocked. I’m not sure that my stop function right or wrong. Can you give me some advice? Thanks
Possible causes for the hang have been suggested by others.
The more important thing however is the actual usage of Thread.Abort.
It is considered a very bad thing to do and you won’t believe the amount of issues and subtle bugs it can cause.
The good news is that you usually don’t really need it, untimely completion of asynchronous tasks can and should be handled by the application itself. Consider your use case for example:
What are you trying to accomplish by calling abort? Are you trying to abort the request? you can’t because it is already under way. it will reach its destination and be processed regardless of whether there is somebody on the other side waiting for it.
If however, you are trying to avoid unnecessary calculations on your side- you have full control. You can keep a Boolean member in the class named “abortRequested” and the Abort method would just set it to “true”. The method that you want to abort needs to check this member from time to time in order to decide whether it should proceed execution.
We can analyze your use case further if you want.
EDIT:
It would look something like this:
You can control the granularity of the ‘stopped’ sampling as you like. However you must be aware that it is not possible to sample it everywhere. For example- you cant check it while the thread is executing a query against a DB. This should be good enough though.