Stoping threads style

  • Thread starter Thread starter Agustin
  • Start date Start date
A

Agustin

Hi. In my application I'm using a thread to asynchronously downlad data
form the web. In the thread I create a Web conection and after download
I rise an event and finish. this is the code

public void Download()
{
WebRequest request = WebRequest.Create(this.Url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
DownloadCompleteEventArgs arg = new DownloadCompleteEventArgs();
arg.Content = responseFromServer;
arg.Url = this.Url;
DownloadComplete(this, arg);
}

The question is, If I want to stop the thread while it's working I just
call the Thread Abort() method? What about the objects created inside
the thread? I thought that may be I should've closed.
How can I check If there is some thread running?
Thank you
 
No, that's not the cleanest way to stop it. Ideally, you'd access the
WebRequest and terminate it from the main thread, causing whatever method is
executing to throw an exception, which you'd catch in the thread routine.
You could then check a flag stored for each such thread to decide if the
attempt should be retried or if the operation was purposely cancelled.

Paul T.
 
Back
Top