Thread ending

J

Jon Slaughter

Since a thread doesn't have a Stop feature and I'm not supose to use abort,
I'm wondering how I stop a thread?

My problem is that I simply want to excute a function in the background and
possibly "restart" it.

What've been doing is essentially:

if (SerialRowThread != null)

{

SerialRowThread.Abort();

WaitingThread.Stop();

}



WaitingThread.Start();




SR.Setup(domainUpDown1.SelectedIndex, Intervals, Entries);

SerialRowThread = new Thread(SR.Compute);

SerialRowThread.Start();



----



SR is the class that contains the method compute and SR.Setup sets up the
state.



now this works but I'm wondering if I can just suspend the thread and it
will do the same but not use abort.



i.e., the GC will end up disposing of the thread because I reassign
SerialRowThread. So it will, hopefully, inherently call abort on the thread
or do whatever clean its suppose to do.

Is this method ok(i.e., calling suspend instead of abort in the above code)
or do I need to use some method of pooling threads? I'm still not to clear
on how a thread shuts down because even in pooling it must be done?

Thanks,

Jon
 
J

Jon Slaughter

hmm... Suspend is depreciated in 2.0 ;/ Now I have no clue how to stop a
thread and release its resources ;/
 
C

Cor Ligthert [MVP]

Jon,

Why you want to restart it, a thread is removed by the system when it is
ready, just start a new thread is more normally done.

Can you tell us therefore what is the reason for you to restart it.

Cor
 
J

Jon Slaughter

Cor Ligthert said:
Jon,

Why you want to restart it, a thread is removed by the system when it is
ready, just start a new thread is more normally done.

Can you tell us therefore what is the reason for you to restart it.


The thread computes some stuff. For simplicity lets suppose it is to compute
the primes between two numbers. Now the user selects the numbers in a gui
and might decide to change them(lets suppose the computation takes long
enough to do so that its not finished before he decides to change).

So I don't want to start another thread that computes the new primes while
the old one is going on. Neither do I want to suspend the old one because
then it stops it in the middle... it doesn't free resources or anything.

I basically want to call GeneratePrimes(a,b); when the user wants to see
them but I need to do it in the background so the user can do some other
stuff. Theres no need, say, for the user to wait 10 mins(hypothetically) if
he realizes that he ment to use the interval [10,1000] instead of [10,100]
which is what he put in. So he either waits or closes the app.

Seems better that when he enters a new interval a new computation is started
and the old one is ended and the results would be disregarded since the user
didn't want them.


One issue, which isn't a problem the way I have it setup, is how the
threads actually work. I think this is called re-entry, what I mean is that
the function might be in the middle of a computation when another thread
starts a new computation without stopping the original thread. Here if its
not programmed properly then bad things could happen. I suppose this is why
locks and stuff are important. But one thing I'm wondering is if the thread
somehow copies the code to a new block so all local data is duplicated.

Although I think that stuff has to do with the apartment model or whatever?

Main thing is I just need to "stop" the thread and "restart" it on new
parameters but everywhere I read says that abort is not the way to go yet
there is no stop method for threads. So maybe the stop is implied by a
suspension of the thread and then no reference to it so that the GC cleans
it up? But the suspend() is depreciated in 2.0 ;/ Seems the only thing I'm
left with is abort ;/

Thanks,
Jon
 
T

Tom Shelton

Jon said:
Cor Ligthert said:
Jon,

Why you want to restart it, a thread is removed by the system when it is
ready, just start a new thread is more normally done.

Can you tell us therefore what is the reason for you to restart it.


The thread computes some stuff. For simplicity lets suppose it is to compute
the primes between two numbers. Now the user selects the numbers in a gui
and might decide to change them(lets suppose the computation takes long
enough to do so that its not finished before he decides to change).

So I don't want to start another thread that computes the new primes while
the old one is going on. Neither do I want to suspend the old one because
then it stops it in the middle... it doesn't free resources or anything.

I basically want to call GeneratePrimes(a,b); when the user wants to see
them but I need to do it in the background so the user can do some other
stuff. Theres no need, say, for the user to wait 10 mins(hypothetically) if
he realizes that he ment to use the interval [10,1000] instead of [10,100]
which is what he put in. So he either waits or closes the app.

Seems better that when he enters a new interval a new computation is started
and the old one is ended and the results would be disregarded since the user
didn't want them.


One issue, which isn't a problem the way I have it setup, is how the
threads actually work. I think this is called re-entry, what I mean is that
the function might be in the middle of a computation when another thread
starts a new computation without stopping the original thread. Here if its
not programmed properly then bad things could happen. I suppose this is why
locks and stuff are important. But one thing I'm wondering is if the thread
somehow copies the code to a new block so all local data is duplicated.

Although I think that stuff has to do with the apartment model or whatever?

Main thing is I just need to "stop" the thread and "restart" it on new
parameters but everywhere I read says that abort is not the way to go yet
there is no stop method for threads. So maybe the stop is implied by a
suspension of the thread and then no reference to it so that the GC cleans
it up? But the suspend() is depreciated in 2.0 ;/ Seems the only thing I'm
left with is abort ;/

Thanks,
Jon

Jon,

A thread stops when it's threadproc exits. There is no need to "stop"
the thread, you just have to exit the threadproc. Most people
encapsulate the thread proc in a class. Then put a stop method on the
class that sets a flag that your proc can poll periodically to know if
it should exit.

So, I would simply signal your primes generator to stop, create a new
one, and start it. Yes, the proc will be on a new thread, but this
isn't really anything to worry about...

As for locks, you shouldn't need to lock local variables, but any
shared resources will have to be locked.
 
M

Michael D. Ober

In your thread, create a local semaphore and make it static. This way all
instances of the thread have access to the same semaphore. Then when your
thread starts, signal this semaphore. Now comes the tricky part - you need
to put in a periodic check during your thread computation that checks the
state of this semaphore and if it's signaled, exit gracefully.

Mike Ober.

Tom Shelton said:
Jon said:
Cor Ligthert said:
Jon,

Why you want to restart it, a thread is removed by the system when it
is
ready, just start a new thread is more normally done.

Can you tell us therefore what is the reason for you to restart it.


The thread computes some stuff. For simplicity lets suppose it is to
compute
the primes between two numbers. Now the user selects the numbers in a gui
and might decide to change them(lets suppose the computation takes long
enough to do so that its not finished before he decides to change).

So I don't want to start another thread that computes the new primes
while
the old one is going on. Neither do I want to suspend the old one because
then it stops it in the middle... it doesn't free resources or anything.

I basically want to call GeneratePrimes(a,b); when the user wants to see
them but I need to do it in the background so the user can do some other
stuff. Theres no need, say, for the user to wait 10 mins(hypothetically)
if
he realizes that he ment to use the interval [10,1000] instead of
[10,100]
which is what he put in. So he either waits or closes the app.

Seems better that when he enters a new interval a new computation is
started
and the old one is ended and the results would be disregarded since the
user
didn't want them.


One issue, which isn't a problem the way I have it setup, is how the
threads actually work. I think this is called re-entry, what I mean is
that
the function might be in the middle of a computation when another thread
starts a new computation without stopping the original thread. Here if
its
not programmed properly then bad things could happen. I suppose this is
why
locks and stuff are important. But one thing I'm wondering is if the
thread
somehow copies the code to a new block so all local data is duplicated.

Although I think that stuff has to do with the apartment model or
whatever?

Main thing is I just need to "stop" the thread and "restart" it on new
parameters but everywhere I read says that abort is not the way to go yet
there is no stop method for threads. So maybe the stop is implied by a
suspension of the thread and then no reference to it so that the GC
cleans
it up? But the suspend() is depreciated in 2.0 ;/ Seems the only thing
I'm
left with is abort ;/

Thanks,
Jon

Jon,

A thread stops when it's threadproc exits. There is no need to "stop"
the thread, you just have to exit the threadproc. Most people
encapsulate the thread proc in a class. Then put a stop method on the
class that sets a flag that your proc can poll periodically to know if
it should exit.

So, I would simply signal your primes generator to stop, create a new
one, and start it. Yes, the proc will be on a new thread, but this
isn't really anything to worry about...

As for locks, you shouldn't need to lock local variables, but any
shared resources will have to be locked.
 
J

Jon Slaughter

A thread stops when it's threadproc exits. There is no need to "stop"
the thread, you just have to exit the threadproc. Most people
encapsulate the thread proc in a class. Then put a stop method on the
class that sets a flag that your proc can poll periodically to know if
it should exit.

So, I would simply signal your primes generator to stop, create a new
one, and start it. Yes, the proc will be on a new thread, but this
isn't really anything to worry about...

As for locks, you shouldn't need to lock local variables, but any
shared resources will have to be locked.


Ok, Thanks. I'll lookin into it some more

Jon
 
J

Jon Slaughter

Michael D. Ober said:
In your thread, create a local semaphore and make it static. This way all
instances of the thread have access to the same semaphore. Then when your
thread starts, signal this semaphore. Now comes the tricky part - you
need to put in a periodic check during your thread computation that checks
the state of this semaphore and if it's signaled, exit gracefully.


I Don't know what exit gracefully means? Do you ment return from the
function call?

i.e.

void Mycomputation()
{
do
{

if (Exit)
{
return;
}


} while (!ComputationFinished)

}

So when a function returns from its call does that "kill" the thread? e.g.
the thread realizes that the function has exited and then the thread will
termintate itself and clean up?


Thanks,
Jon
 
M

Marc Gravell

In some circumstances (e.g. math-intensive pure managed code) that may well
be enough. The main point is that the method should exit cleanly, i.e. it
should attempt to not leave a hige mess behind itself. This can be largely
mitigated by correct use of "using" anf "try/finally" constructs, such that
critical code (i.e. "close the file", "disconnect from server") is executed
when they abandon the thread.

And yes; threads clean themselves up appropriately (or just free themselves
up if pooled) simply by leaving the ThreadStart (etc) method.

Unfortunately, fitting threading code at the *end* a project is not the easy
route.

Marc
 

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