Stoping threads style

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
 
P

Paul G. Tobey [eMVP]

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.
 

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