471,594 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Thread ending

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
Nov 22 '06 #1
10 2039

hmm... Suspend is depreciated in 2.0 ;/ Now I have no clue how to stop a
thread and release its resources ;/
Nov 22 '06 #2
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

"Jon Slaughter" <Jo***********@Hotmail.comschreef in bericht
news:4X***************@newssvr25.news.prodigy.net. ..
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


Nov 22 '06 #3

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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
Nov 22 '06 #4

Jon Slaughter wrote:
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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.

--
Tom Shelton

Nov 22 '06 #5
Jon Slaughter <Jo***********@Hotmail.comwrote:
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?
See http://www.pobox.com/~skeet/csharp/t...shutdown.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 22 '06 #6
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" <to*********@comcast.netwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>
Jon Slaughter wrote:
>"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...
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.

--
Tom Shelton

Nov 22 '06 #7

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Jon Slaughter <Jo***********@Hotmail.comwrote:
>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?

See http://www.pobox.com/~skeet/csharp/t...shutdown.shtml
I've been reading over that and I'm trying to understand it all in a
comprehensible way. The problem is I already created my thread for my app
and pretty much finished the app and I didn't want to have to change to
much. I was hoping that there was a simple way to modify my code to work
properly.

Thanks,
Jon
Nov 22 '06 #8

"Tom Shelton" <to*********@comcast.netwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
<snip>
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
Nov 22 '06 #9

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:e8**************@TK2MSFTNGP02.phx.gbl...
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
Nov 22 '06 #10
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
Nov 22 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Olivier Parisy | last post: by
2 posts views Thread by ice88 | last post: by
8 posts views Thread by Jason Chu | last post: by
3 posts views Thread by Raj Wall | last post: by
27 posts views Thread by Ritesh Raj Sarraf | last post: by
3 posts views Thread by Chip | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.