473,320 Members | 1,846 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,320 software developers and data experts.

How do I gently terminate a thread???

Hello,

coming from win32 API I recall an ExitThread() call to gently terminate a
thread from inside the same thread .... but now all I can see is an Abort
call which seems to me a wrapper on the TerminateThread() Win32 API which is
a brutal way to end a thread. Is there any other more gentle way to close a
thread???

Bob Rock

Jul 21 '05 #1
11 2177
Bob,

You ^could^ call the ExitThread API to exit the thread. However, the
managed concept of a Thread and the actual implementation may not always be
in synch (also, if this thread is from the thread pool, then the results are
unpredictable as well). It is possible that in the future that fibers will
be used to support the managed concept of a thread, for example.

If anything, I would use return values from a method or some other
indicator to exit a thread, not using ExitThread. It just seems like a bad
implementation to "bash" the thread like that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Hello,

coming from win32 API I recall an ExitThread() call to gently terminate a
thread from inside the same thread .... but now all I can see is an Abort
call which seems to me a wrapper on the TerminateThread() Win32 API which is a brutal way to end a thread. Is there any other more gentle way to close a thread???

Bob Rock

Jul 21 '05 #2
Bob Rock wrote:
coming from win32 API I recall an ExitThread() call to gently
terminate a thread from inside the same thread ....
You never should call ExitThread directly, because this normaly leads to
memory leaks (at least from the CRT).
but now all I can
see is an Abort call which seems to me a wrapper on the
TerminateThread() Win32 API which is a brutal way to end a thread. Is
there any other more gentle way to close a thread???


return !?

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
Jul 21 '05 #3
Say for instance I have a thread that I fire off that's running some loop of
code that's monitoring status or something. I know I could define a
variable in a shared address space, and have that loop until the variable
goes false or something, which would then have the thread exit. Is there a
way though without using this method to cause a thread to shut down without
calling Abort() ?
Ryan Gregg
"Jochen Kalmbach" <no********************@holzma.de> wrote in message
news:Xn*********************************@127.0.0.1 ...
Bob Rock wrote:
coming from win32 API I recall an ExitThread() call to gently
terminate a thread from inside the same thread ....


You never should call ExitThread directly, because this normaly leads to
memory leaks (at least from the CRT).
but now all I can
see is an Abort call which seems to me a wrapper on the
TerminateThread() Win32 API which is a brutal way to end a thread. Is
there any other more gentle way to close a thread???


return !?

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/

Jul 21 '05 #4
Hi,

"Ryan Gregg" <rg****@wheatlandsystems.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Say for instance I have a thread that I fire off that's running some loop of code that's monitoring status or something. I know I could define a
variable in a shared address space, and have that loop until the variable
goes false or something, which would then have the thread exit. Is there a way though without using this method to cause a thread to shut down without calling Abort() ?


You could throw an exception that is only caught in the main method of the
your thread, then just return.

Or, if you wait on WaitHandle-s, you join with other threads or you sleep a
lot (Thread.Sleep), you could call Thread.Interrupt. The next wait, join or
sleep on the thread will then throw a ThreadInterruptedException. Beware of
general exception handlers that could catch this exception and ignore it.

The nice thing about Thread.Abort is that it throws a
ThreadAbortedException, which is a special kind of exception. It will be
rethrown when caught until the thread exits, or until you call
Thread.ResetAbort.

Cheers,
---
Tom Tempelaere.

Jul 21 '05 #5
I would check a bool or enum (running, stopped, paused, etc) at the top of
the thread loop and exit if the bool or enum is stopped. This var needed to
be synced if multiple threads get/set it (i.e. reader is your worker, writer
is your caller.) This is the first best way for the standard path.
Sometimes this may not be checked as you could be waiting for event or
blocking on i/o, etc. One reason to use timeouts on wait operations. In
your Stop method, set the bool and wait some period of time to allow thread
to stop, calling join(timeout) with some reasonable timeout. If it does not
join, then your next step could be thread.abort() and another join, maybe in
a loop to do 1 or more times. Your worker thread should catch the exception
and allow you to run finally, etc. and reset the thread.abort reset deal and
exit as cleanly as it can in code.

--
William Stacey, MVP

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Hello,

coming from win32 API I recall an ExitThread() call to gently terminate a
thread from inside the same thread .... but now all I can see is an Abort
call which seems to me a wrapper on the TerminateThread() Win32 API which is a brutal way to end a thread. Is there any other more gentle way to close a thread???

Bob Rock


Jul 21 '05 #6
> You could throw an exception that is only caught in the main method of the
your thread, then just return.


Probably a stupid question, but, how do you throw and exception from a
thread (the main thread) that needs to be caught by another thread (the
worker thread)????

Bob Rock
Jul 21 '05 #7

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:en****************@TK2MSFTNGP11.phx.gbl...
You could throw an exception that is only caught in the main method of the your thread, then just return.


Probably a stupid question, but, how do you throw and exception from a
thread (the main thread) that needs to be caught by another thread (the
worker thread)????


could Thread.Interrupt() do that?
Jul 21 '05 #8
from a pure visual c,native programmer,
maybe this can help,
I know dotnet has more to offer, but in this case I think it's same thing...

at design time, if the thread to end does some sort of loop/polling, in that
loop check for a global flag that when changes status, makes it exit,
you'd change status from another thread.
you can use enevts too if you will.
your thread-to-end may be simple doing a WaitForSingleObject/MultipleObject
on a given global event handle.

there isn't a nice way to 'kill' 'end' 'terminate' a thread.
unfortunately, if you dont design the software to end gracefully a thread.
the TerminateThread is the only solution but you'll have 1MB stack lost for
sure, and probably some leaks and curruption.

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Hello,

coming from win32 API I recall an ExitThread() call to gently terminate a
thread from inside the same thread .... but now all I can see is an Abort
call which seems to me a wrapper on the TerminateThread() Win32 API which is a brutal way to end a thread. Is there any other more gentle way to close a thread???

Bob Rock

Jul 21 '05 #9
Standard way to "gently" terminated thread is to use return; in thread body
(in ThreadStart delegate method).
That's why you can't find specific method.

HTH
Alex

.....
there isn't a nice way to 'kill' 'end' 'terminate' a thread.
unfortunately, if you dont design the software to end gracefully a thread.
the TerminateThread is the only solution but you'll have 1MB stack lost for sure, and probably some leaks and curruption.

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Hello,

coming from win32 API I recall an ExitThread() call to gently terminate a thread from inside the same thread .... but now all I can see is an Abort call which seems to me a wrapper on the TerminateThread() Win32 API
which is
a brutal way to end a thread. Is there any other more gentle way to
close a
thread???

Bob Rock


Jul 21 '05 #10
Hi,

"spammy" <me@privacy.net> wrote in message
news:2g**********@uni-berlin.de...

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:en****************@TK2MSFTNGP11.phx.gbl...
You could throw an exception that is only caught in the main method of the your thread, then just return.


Probably a stupid question, but, how do you throw and exception from a
thread (the main thread) that needs to be caught by another thread (the
worker thread)????


could Thread.Interrupt() do that?


Yes.

Interrupt called on a certain Thread object, will throw an exception on the
actual thread that the object represents, on the next wait, sleep or join
operation performed on that thread, or if the thread is already waiting,
joining or sleeping (WaitHandle::Waitxxx, Thread::Sleep, Thread::Join). The
exception is of type ThreadInterruptedException.

Abort called on a certain Thread object, will throw an exception on the
actual thread that the object represents, period. The exception is of type
ThreadAbortedException. There is one exception (no pun). The framework
cannot throw the exception if the thread is executing interop code (i.c. it
can only be thrown from managed code).

It does not matter from which thread or on what Thread object you call Abort
or Interrupt, so you can call these methods on the current thread being
executed. Calling Thread.Abort from the current thread will surely throw a
ThreadAbortedException. You can easily verify, just execute the following on
a thread:

Thread.CurrentThread.Abort();

Add a catch handler for the exception, you will see.

Cheers,
---
Tom Tempelaere
Jul 21 '05 #11
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:OW***************@TK2MSFTNGP09.phx.gbl...
Standard way to "gently" terminated thread is to use return; in thread body (in ThreadStart delegate method).
That's why you can't find specific method.

HTH
Alex


Or like I said before, just throw a specific exception. If the thread is not
just a simple loop, but has a recursive pattern or is just more complex, and
you don't want to manually check each return-value for an and-thread signal,
it programs cleaner to just throw an exception.

Tom.
Jul 21 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dr. J | last post by:
How to terminate a blocked thread? In my form's "load" I launch a TCP listening thread that stays in an infinite loop waiting for incoming TCP packets. In this form's "closing" I try to...
11
by: Bob Rock | last post by:
Hello, coming from win32 API I recall an ExitThread() call to gently terminate a thread from inside the same thread .... but now all I can see is an Abort call which seems to me a wrapper on the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.