ending a thread

  • Thread starter Thread starter RobcPettit
  • Start date Start date
R

RobcPettit

Hi, Im using
public void threadcontrol()
{

Thread scan = new Thread (new ThreadStart(GethtmlStream));
scan.Start();
name = "Scanning";
}
in a class, and calling it from button1 with
BJresults sr = new BJresults();
sr.threadcontrol();

np problems here, thread starts and does what it supposed to do. I want
to be able to stop the thread from button2 and I cant figure out how
to. Any ideas.
Regars Robert
 
Not sure but maybe try making the thread a member variable instead of a
local variable. That way you can call it outside the method to stop it.
 
Use a bool flag to signal when it's time for the thread to shutdown
gracefully. The following article explains the pattern in detail.

<http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml>

If your worker thread spends a lot of time blocked on a WaitHandle then
you'd probably want to signal that WaitHandle in conjunction with
setting a flag. I suppose you could also use a separate WaitHandle in
addition to the existing WaitHandle that would signal the thread to
shutdown. In that case the thread could block for either signal.
You'd use WaitHandle.WaitAny to do that.

Brian
 
Thankyou both for your replys. Ill read through the link given,
thankyou for this. I will have to read up on what a member variable is
as im still a begginer. Basically the class Ive written is to read data
from a text file, go to a web site(relevent to data in text file) read
the html into stringbuilder and parse the relevent data to a text file.
Theres something like 75000 records to gather.
Regards Robert
 
Thankyou both for your replys. Ill read through the link given,
thankyou for this. I will have to read up on what a member variable is
as im still a begginer. Basically the class Ive written is to read data
from a text file, go to a web site(relevent to data in text file) read
the html into stringbuilder and parse the relevent data to a text file.
Theres something like 75000 records to gather.
Regards Robert

Member variable = variable that can be accessed anywhere in your class.
local variable = variable can only be accessed within a method.

Sounds like you just need to write a console app that will exit when
complete. If this is the case you wouldn't even need to create a
thread unless you need to do multiple things at once.
 
Thanks again for your replys. I originally wrote this as a console
application, but i needed to get feed back on how many records have
been processed. Because Im looking at proccessing over 75000 records, I
need to stop the thread and continue another day. In other posts I was
advised to take the threading route. So far Ive learnt how to write a
thread and start the thread, which Im really pleased with getting this
far. I have to say for a begginer its a big topic, and Ive along way to
go. Once Im able to stop the thread, I need to add a errorhandler as
well, then I need to see how to get feed back about records processed.
Ive been reading up on this and that doesnt look to bad. In fact the
only reason I realised I needed to stop the thread is I clossed do
VS2005, and saw the thread was still running and had to end it in
procceses. I would have struglled more without these replys. Thanks
again.
Regards Robert
 
Just out of curiousity, why would you use the bool flag in conjunction
with the waithandle? Why isn't the wait handle enough? Access is
synchronized already and you also have the added benefit that you can
use the event globally to shutdown all worker threads.

I usually use a pattern similar to this:

//thread work routine:
public void Start()
{


//the time can be increased if the thread is more of a
"polling" object
while(!_manualEvent.WaitOne(1,true))
{
//do work here, make sure it's broken up into small
enough chunks that
//the event is checked continually

}



}
 
I was referring to scenarios where a thread may already be using a
WaitHandle for purposes other than shutting down. If you just used the
existing WaitHandle then how would the thread know that it was suppose
to shut down instead of doing the intended operation?

In your example I would just use a bool flag instead of the
ManualResetEvent especially if there is no reason for the thread to
sleep. But, you bring up a good point in the comments about threads
that wake up on fixed intervals to do work. In that case I think your
example is fine. In fact, that's what I do as well. I didn't even
think to mention that common scenario.

Brian
 

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

Similar Threads

wrong 2
Stopping a listening thread 2
threading 2
Threading: UI update from another namespace. 6
Thread 1
GUI Thread - Is cross-thread operation really bad? 19
Thread GC collection 6
timer/threading 10

Back
Top