views:

583

answers:

7

So I know that you shouldn't use

Thread.Abort()

But I've never been given a good explanation. Is there a performance penalty or some hidden gotcha?

I know you can't ignore/swallow the ThreadAbortException (which makes sense)

A: 

Thread.Abort stops your thread in an uncontrolled fashion. thread.Abort will throw an exception, which will cause that your thread stops immediatly.

What is wrong with that: in most cases, you want to gracefully stop the operation that you're performing. For instance, if you are executing an ACID operation, you might want to complete the current operation before ending the thread, so that your system remains in a stable state.

Frederik Gheysels
A: 

When you call Thread.Abort() on another thread a ThreadAbortException is injected in the flow of that thread. If you're lucky the code will handled this well and abort in a well defined state. The problem is that you have no way to figure out if you will be lucky in every case, so if you prefer safe over sorry calling Thread.Abort on other threads is not a good idea.

Brian Rasmussen
+5  A: 

Because if you know that the thread is in some safe state in which it can be aborted, surely you can arrange better communication and have the thread exit cleanly.

The thread could have taken a lock and be in the middle of changing some shared state, and the Thread.Abort will undo the lock and leave the shared state corrupted.

Damien_The_Unbeliever
This can be prevented with the use of http://msdn.microsoft.com/en-us/library/system.threading.thread.begincriticalregion.aspx, although I'd consider this quite brittle!
Rob Fonseca-Ensor
It is possible to wrap the body of your thread in a try/finally and them clean up your resources in the finally block.But in general I see what you mean.
Jan Bannister
A: 

Thread.Abort rises an exception in the target thread. Target thread in the meantime can be performing some critical operations and rising an exception can break your application state.

Vitaliy Liptchinsky
A: 

In short. Any IDisposable object may not be disposed. Any locked object may not be unlocked. Anything that must be 100% performed will never be done.

Vasiliy Borovyak
Anything that must be 100% performed will fail sooner or later due to a power outage...
gimpf
A: 

It's easier to hurt yourself. As others have stated it raises an exception in the code, which can occur at any point. This might be fine if you expect this and have coded in a way that elegantly handles this exception at any point but some people dont:

Monitor.Enter(obj);
// some code - if exception is raised here, then the lock isn't released
Monitor.Exit(obj)

IDisposable someCriticalResource = GetResource();
// some code - if exception is raised here, then the object isn't disposed
someCriticalResource.Dispose();

Additionally if you're working with many people on a team unless you have good code reviews you cannot guarantee the quality of the code you'll be working with. Hence it is a good idea to preach the gospal of "no Thread.Abort()" than it is to get people to remember to write code that is robust against exceptions occuring anywhere within that code.

Quibblesome
+7  A: 

In addition to all of the other good answers here, let me add that there is no guarantee whatsoever that a call to Thread.Abort will actually abort the thread in question, ever. It is possible (though not particularly easy) to "harden" a thread against being aborted. If, for example, you are aborting a thread because you believe it to be running hostile code then the hostile code could be resisting its own destruction.

If you have a long-running operation involving code that you do not own that must be taken down cleanly, the correct way to do this is to put that code in its own process, not its own thread. (And preferably in a highly security-restricted appdomain in that process.) You can then cleanly kill the process.

In short, Thread.Abort is at best indicative of bad design, possibly unreliable, and extremely dangerous. It should be avoided at all costs; the only time you should ever even consider aborting a thread is in some sort of "emergency shutdown" code where you are attempting to tear down an appdomain as cleanly as possible.

Eric Lippert
I have one of the situations you highlight. Which is someone elses code (an IronPython Console) that has a Run(...) function which is expecting to be run in a thread.BTW, Do you have an example of 'hardening' a Thread?
Jan Bannister
Do a web search on "Constrained Execution Regions" and you'll learn more than you want to know about how to mess around with thread abort exception propagation.
Eric Lippert