473,385 Members | 1,642 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,385 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 2182
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.