473,395 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 2162

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olivier Parisy | last post by:
Hi, I like to use thread to simplify the handling of independant, blocking tasks. But controling them from a main thread is not always easy to do in a clean way. So I've written some generic...
2
by: ice88 | last post by:
I would like to execute some code when a thread terminates, in the context of that thread - I guess similar to an ExitThread handler - is it possible in a C# .NET application? I can see a way to...
6
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: ...
1
by: GSK | last post by:
I am logging the following error when re-directing via Response.Redirect: "Thread was being aborted at System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object...
8
by: Jason Chu | last post by:
I have a webpage which uploads a big file onto access db. if the file is say around 30 megs, it'll take around a minute for it to get put into the access db. I didn't want the user to wait for it,...
3
by: Raj Wall | last post by:
Hi, I have an application that uses a number of sub-threads. What is the best way to do some processing in each thread when the main application is shut down? Is the ThreadAbortException...
27
by: Ritesh Raj Sarraf | last post by:
Hi, I have some basic doubts about thread. I have a list which has items in it which need to be downloaded from the internet. Let's say list is: list_items which has 100 items in it.
3
by: Chip | last post by:
I need to have an asynch thread abort itself. Something akin to a STOP. The thread that spawned it cannot access it so it must done within the thread. Chip
6
by: Lars Uffmann | last post by:
In an event routine, I want to end a certain thread. I am setting a flag that is checked by the thread and causes it to end, when it is set. Then the thread sets a "response" flag, just before...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.