473,397 Members | 2,084 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,397 software developers and data experts.

Thread.Abort - is it safe?

Hi folks,
You know, the Thread class has got a method named Abort which
according to the msdn:

"Raises a ThreadAbortException in the thread on which it is invoked,
to begin the process of terminating the thread. Calling this method
usually terminates the thread."

I've had a long discussion with someone on not to use the mentioned
method unless under the most extreme cases. I believe that it's
equivalent to the TerminateThread Win32 API and everything that
applies to the mentioned Win32 API, is also in place when using the
Thread.Abort method. When the Abort method is executed, the target
thread has no chance to execute any user-mode code and its initial
stack is not deallocated. On the other hand, DLLs attached to the
thread are not notified that the thread is terminating. And therefore
the use of the mentioned method is completely at your own risk.

Correct me if I'm wrong.

Thank in advance.
Mehdi

Jul 25 '07 #1
6 2920
Read these:
http://www.sellsbrothers.com/askthew...dAbortExce.htm
http://forums.microsoft.com/MSDN/Sho...94181&SiteID=1
"mehdi" <me***********@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
Hi folks,
You know, the Thread class has got a method named Abort which
according to the msdn:

"Raises a ThreadAbortException in the thread on which it is invoked,
to begin the process of terminating the thread. Calling this method
usually terminates the thread."

I've had a long discussion with someone on not to use the mentioned
method unless under the most extreme cases. I believe that it's
equivalent to the TerminateThread Win32 API and everything that
applies to the mentioned Win32 API, is also in place when using the
Thread.Abort method. When the Abort method is executed, the target
thread has no chance to execute any user-mode code and its initial
stack is not deallocated. On the other hand, DLLs attached to the
thread are not notified that the thread is terminating. And therefore
the use of the mentioned method is completely at your own risk.

Correct me if I'm wrong.

Thank in advance.
Mehdi

Jul 25 '07 #2
by the way mehdi,
Thread.Abort gives more headaches than what that win32 API function would.
In fact the win32 api is simpler because you actually know that its not good
to use it.

the Thread.Abort( ) is more complicated.
you need to remember that if its thrown when the code is an finalize block,
it does not run the rest of code from it.
also this exception is automatically rethrown at the end of a catch block.

i tried to mimic Thread.Abort in C++ code. i wrote an Abort(HANDLE hThread)
procedure which queues an APC to the thread. the APC proc is throwing an
exception.

Jul 25 '07 #3
Medhi,

Calling Abort on a thread is different in .NET.

First, you have to understand that a thread is a logical construct in
..NET, not always guaranteed to be connected to a physical thread. While for
the most part when you run your apps, there is a distinct physical thread
that correlates to your Thread instance (which represents a logical thread),
there is no guarantee that will be the case. It's also dependent on the CLR
host. SQL Server, as a CLR host, for example, might run different logical
threads on the same thread, or even fibers.

Assuming that you are in a regular app though, Abort isn't going to call
TerminateThread. The CLR will wait for any interop call to complete on that
thread (if there is one currently taking place) and then a
ThreadAbortException will be thrown. Based on this, I'm assuming that the
exception will unwind the stack, and then the thread will simply stop
executing. TerminateThread doesn't need to be called because the thread has
stopped terminating at that point.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mehdi" <me***********@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
Hi folks,
You know, the Thread class has got a method named Abort which
according to the msdn:

"Raises a ThreadAbortException in the thread on which it is invoked,
to begin the process of terminating the thread. Calling this method
usually terminates the thread."

I've had a long discussion with someone on not to use the mentioned
method unless under the most extreme cases. I believe that it's
equivalent to the TerminateThread Win32 API and everything that
applies to the mentioned Win32 API, is also in place when using the
Thread.Abort method. When the Abort method is executed, the target
thread has no chance to execute any user-mode code and its initial
stack is not deallocated. On the other hand, DLLs attached to the
thread are not notified that the thread is terminating. And therefore
the use of the mentioned method is completely at your own risk.

Correct me if I'm wrong.

Thank in advance.
Mehdi

Jul 25 '07 #4
On Jul 25, 6:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Medhi,

Calling Abort on a thread is different in .NET.

First, you have to understand that a thread is a logical construct in
.NET, not always guaranteed to be connected to a physical thread. While for
the most part when you run your apps, there is a distinct physical thread
that correlates to your Thread instance (which represents a logical thread),
there is no guarantee that will be the case. It's also dependent on the CLR
host. SQL Server, as a CLR host, for example, might run different logical
threads on the same thread, or even fibers.

Assuming that you are in a regular app though, Abort isn't going to call
TerminateThread. The CLR will wait for any interop call to complete on that
thread (if there is one currently taking place) and then a
ThreadAbortException will be thrown. Based on this, I'm assuming that the
exception will unwind the stack, and then the thread will simply stop
executing. TerminateThread doesn't need to be called because the thread has
stopped terminating at that point.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"mehdi" <mehdi.mous...@gmail.comwrote in message

news:11**********************@o61g2000hsh.googlegr oups.com...
Hi folks,
You know, the Thread class has got a method named Abort which
according to the msdn:
"Raises a ThreadAbortException in the thread on which it is invoked,
to begin the process of terminating the thread. Calling this method
usually terminates the thread."
I've had a long discussion with someone on not to use the mentioned
method unless under the most extreme cases. I believe that it's
equivalent to the TerminateThread Win32 API and everything that
applies to the mentioned Win32 API, is also in place when using the
Thread.Abort method. When the Abort method is executed, the target
thread has no chance to execute any user-mode code and its initial
stack is not deallocated. On the other hand, DLLs attached to the
thread are not notified that the thread is terminating. And therefore
the use of the mentioned method is completely at your own risk.
Correct me if I'm wrong.
Thank in advance.
Mehdi
Well, so you mean if I get a handle to a WDM driver, for instance, in
a newly created thread, and then aborting the thread execution using
the Abort method, the handle will be also released, automagically? and
the driver will be successfully unloaded? How are we supposed to know
the subtle nuances of how the Abort method works?

On the other hand, is there anyway to be notified whether a given
Win32 API is called? (maybe a Win32 API monitor or the like).

Thank you for your time,
Mehdi

Jul 25 '07 #5
medhi,

As far as getting a handle to a WDM driver, if you are in the middle of
an interop call, then the call to Abort will wait for that. After the call,
the exception will be thrown. If you are properly disposing of the any
handles you have in a finally block (whether explicit or through the using
statement) then it should dispose of properly.

However, if you are in a finally block already and the code to release
any unmanaged handles has not excecuted yet, then Abort is called, then
those handles will not be released.

If you want to make sure that the DLL that you called through the
P/Invoke layer is released, I think you will have to call FreeLibrary, using
a module handle that you obtain through a call to GetModuleHandle. I don't
know how the P/Invoke layer will take this though, so you might want to be
careul in doing that.
All-in-all, calling Abort is just a bad idea, it creates too many "what
ifs". Instead of trying to use abort, why not have checks in the thread
code every so often to see if you should be exiting the thread?

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

"mehdi" <me***********@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
On Jul 25, 6:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>Medhi,

Calling Abort on a thread is different in .NET.

First, you have to understand that a thread is a logical construct in
.NET, not always guaranteed to be connected to a physical thread. While
for
the most part when you run your apps, there is a distinct physical thread
that correlates to your Thread instance (which represents a logical
thread),
there is no guarantee that will be the case. It's also dependent on the
CLR
host. SQL Server, as a CLR host, for example, might run different
logical
threads on the same thread, or even fibers.

Assuming that you are in a regular app though, Abort isn't going to
call
TerminateThread. The CLR will wait for any interop call to complete on
that
thread (if there is one currently taking place) and then a
ThreadAbortException will be thrown. Based on this, I'm assuming that
the
exception will unwind the stack, and then the thread will simply stop
executing. TerminateThread doesn't need to be called because the thread
has
stopped terminating at that point.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"mehdi" <mehdi.mous...@gmail.comwrote in message

news:11**********************@o61g2000hsh.googleg roups.com...
Hi folks,
You know, the Thread class has got a method named Abort which
according to the msdn:
"Raises a ThreadAbortException in the thread on which it is invoked,
to begin the process of terminating the thread. Calling this method
usually terminates the thread."
I've had a long discussion with someone on not to use the mentioned
method unless under the most extreme cases. I believe that it's
equivalent to the TerminateThread Win32 API and everything that
applies to the mentioned Win32 API, is also in place when using the
Thread.Abort method. When the Abort method is executed, the target
thread has no chance to execute any user-mode code and its initial
stack is not deallocated. On the other hand, DLLs attached to the
thread are not notified that the thread is terminating. And therefore
the use of the mentioned method is completely at your own risk.
Correct me if I'm wrong.
Thank in advance.
Mehdi

Well, so you mean if I get a handle to a WDM driver, for instance, in
a newly created thread, and then aborting the thread execution using
the Abort method, the handle will be also released, automagically? and
the driver will be successfully unloaded? How are we supposed to know
the subtle nuances of how the Abort method works?

On the other hand, is there anyway to be notified whether a given
Win32 API is called? (maybe a Win32 API monitor or the like).

Thank you for your time,
Mehdi

Jul 25 '07 #6
On Jul 25, 7:25 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
medhi,

As far as getting a handle to a WDM driver, if you are in the middle of
an interop call, then the call to Abort will wait for that. After the call,
the exception will be thrown. If you are properly disposing of the any
handles you have in a finally block (whether explicit or through the using
statement) then it should dispose of properly.

However, if you are in a finally block already and the code to release
any unmanaged handles has not excecuted yet, then Abort is called, then
those handles will not be released.

If you want to make sure that the DLL that you called through the
P/Invoke layer is released, I think you will have to call FreeLibrary, using
a module handle that you obtain through a call to GetModuleHandle. I don't
know how the P/Invoke layer will take this though, so you might want to be
careul in doing that.

All-in-all, calling Abort is just a bad idea, it creates too many "what
ifs". Instead of trying to use abort, why not have checks in the thread
code every so often to see if you should be exiting the thread?

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"mehdi" <mehdi.mous...@gmail.comwrote in message

news:11*********************@w3g2000hsg.googlegrou ps.com...
On Jul 25, 6:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Medhi,
Calling Abort on a thread is different in .NET.
First, you have to understand that a thread is a logical construct in
.NET, not always guaranteed to be connected to a physical thread. While
for
the most part when you run your apps, there is a distinct physical thread
that correlates to your Thread instance (which represents a logical
thread),
there is no guarantee that will be the case. It's also dependent on the
CLR
host. SQL Server, as a CLR host, for example, might run different
logical
threads on the same thread, or even fibers.
Assuming that you are in a regular app though, Abort isn't going to
call
TerminateThread. The CLR will wait for any interop call to complete on
that
thread (if there is one currently taking place) and then a
ThreadAbortException will be thrown. Based on this, I'm assuming that
the
exception will unwind the stack, and then the thread will simply stop
executing. TerminateThread doesn't need to be called because the thread
has
stopped terminating at that point.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"mehdi" <mehdi.mous...@gmail.comwrote in message
>news:11**********************@o61g2000hsh.googleg roups.com...
Hi folks,
You know, the Thread class has got a method named Abort which
according to the msdn:
"Raises a ThreadAbortException in the thread on which it is invoked,
to begin the process of terminating the thread. Calling this method
usually terminates the thread."
I've had a long discussion with someone on not to use the mentioned
method unless under the most extreme cases. I believe that it's
equivalent to the TerminateThread Win32 API and everything that
applies to the mentioned Win32 API, is also in place when using the
Thread.Abort method. When the Abort method is executed, the target
thread has no chance to execute any user-mode code and its initial
stack is not deallocated. On the other hand, DLLs attached to the
thread are not notified that the thread is terminating. And therefore
the use of the mentioned method is completely at your own risk.
Correct me if I'm wrong.
Thank in advance.
Mehdi
Well, so you mean if I get a handle to a WDM driver, for instance, in
a newly created thread, and then aborting the thread execution using
the Abort method, the handle will be also released, automagically? and
the driver will be successfully unloaded? How are we supposed to know
the subtle nuances of how the Abort method works?
On the other hand, is there anyway to be notified whether a given
Win32 API is called? (maybe a Win32 API monitor or the like).
Thank you for your time,
Mehdi
Thank you Nicholas,
I've never ever used the Abort method, even once. I was just trying to
get a straight answer to the above question, for someone else. Since,
I couldn't convince him that calling the Abort method is not a safe-
practice; exactly because of those "what ifs" you've already
mentioned.

Thank you again for your time,
Mehdi

Jul 25 '07 #7

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

Similar topics

14
by: Daisy | last post by:
From this page: http://www.c-sharpcorner.com/2/mt_beginner1.asp Thread class's Abort method is called to kill a thread permanently. Make sure you call IsAlive before Abort. if (...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
1
by: Robin Tucker | last post by:
Hi there, I have a "worker thread", which can perform one of many tasks, including fetching and sending data blobs to a database, load files etc. Now, a progress dialog is displayed while the...
4
by: fred | last post by:
I use a Synclock in a secondary thread and also stop the thread using the abort method. If the abort occurs while the thread is in the Synclock will the SyncLock always be released before the...
3
by: jmd.msdn | last post by:
Hello. I want to abort a thread whose thread function contains something like : void threadfunc () { while ( 1 ) { ... some code udpClient.Receive(...); // instance to read network data...
6
by: foolmelon | last post by:
If a childThread is in the middle of a catch block and handling an exception caught, the main thread calls childThread.Abort(). At that time a ThreadAbortException is thrown in the childThread. ...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
20
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post by:
John Dohn wrote: When I do this, I put a special value in the queue (like None) and in the worker thread, check for the special value and exit if found. Threads can also be marked as "daemon...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.