How To Stop a Thread in .NET (and Why Thread.Abort is Evil)

Ian Griffiths (one of my favorite tech bloggers) wrote this fine piece on why Thread.Abort is a representation of all that is evil and threatens the American (and British) way of life.

The problem with Thread.Abort is that it can interrupt the progress of the target thread at any point. It does so by raising an ’asynchronous’ exception, an exception that could emerge at more or less any point in your program. (This has nothing to do with the .NET async pattern by the way - that’s about doing work without hogging the thread that started the work.)

If you’re interested in how Thread.Abort raises an exception in another thread, read Chris Sells’ (another favorite blogger) investigative report here.

I’ve taken this to heart in the design of my Socket server class (which I will release to the public some day) and in any situation where I have a service running that spawns asynchronous operations. Ian’s appoach to cancelling an asynchronous operation is the similar to mine:

The approach I always recommend is dead simple. Have a volatile bool field that is visible both to your worker thread and your UI thread. If the user clicks cancel, set this flag. Meanwhile, on your worker thread, test the flag from time to time. If you see it get set, stop what you’re doing.

One difference is that I chose not to use a volatile bool field. My reasoning was that if my asynchronous operation only reads the value (and never writes it) and just happened to be reading it while my main thread was changing it to false (in response to a user cancellation effort), I’m not so concerned that asynchronous operation might read true even though it’s being set to false. Why not? Well it’ll stay false by the time I check it again and the chance of that small synchronization flaw is very minute and has a low cost even if it does occur.

The question is, am I missing something more important by not using a volatile field in this instance?

Technorati Tags:

What others have said

Requesting Gravatar... you've been HAACKED Nov 18, 2004 10:28 AM
# Quiz Answer: Watch out for the Eeeevil Thread.Abort.
Requesting Gravatar... Mr. Something Jun 07, 2005 1:23 PM
# re: How To Stop a Thread in .NET (and Why Thread.Abort is Evil)
Yes, you could be missing something. To use Java as an example, I believe the volatile keyword forces synchronization of the value between threads because each thread has it's own copy of all variables in CPU cache. There could be an issue if you have multiple physical processors and the thread you set the flag false in is on a different processor then the one you're signaling. In that case, (unless I'm off) it's possible there would be a sizeable delay or that the variable change might never make it to the other thread.
Requesting Gravatar... OwfAdmin Jul 10, 2007 12:46 AM
# re: How To Stop a Thread in .NET (and Why Thread.Abort is Evil)
try this useful link:
http://msdn.microsoft.com/msdnmag/issues/06/03/NETMatters/
Requesting Gravatar... saransony Dec 06, 2007 11:37 PM
# re: How To Stop a Thread in .NET (and Why Thread.Abort is Evil)
.NET allows to call System.Windows.Forms.Control functions only from thread where control was created. To run them from other thread we need to use Control.Invoke (synchronous call) or Control.BeginInvoke (asynchronous call) functions. For task like showing database records we need Invoke. To implement this :Delegate type for calling form function, delegate instance and function called using this delegate Invoke call from worker thread. Next problem is to stop worker thread correctly. Steps to do this:Set event "Stop Thread" Wait for event "Thread is stopped" While waiting for event process messages using Application.DoEvents function. This prevents deadlock because worker thread makes Invoke calls which are processed in main thread. Thread function checks in every iteration whether Stop Thread event is set. If event is set, function makes clean-up operations, sets event "Thread is stopped" and returns.
try this useful link---
http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html
http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation

Requesting Gravatar... chathuranga Dec 14, 2007 3:08 AM
# re: How To Stop a Thread in .NET (and Why Thread.Abort is Evil)
I have an array of threads working on some task... and I wan to end any thread if it's taking time to execute... so what I did was using a Timer and it's event.. called Thread.Abort() on threads taking more time..but as u mention, with the Abort()..s unexpected behavior.. it's hard to expect what I want to succeed. Works most of the time.. can't rely this. Any ideas.
the way proposed here seems not applicable here for me bcoz the specific thread is working on a time taking method call and no way to repeatedly check a variable to end the thread itself
Requesting Gravatar... mick Apr 09, 2008 10:55 PM
# re: How To Stop a Thread in .NET (and Why Thread.Abort is Evil)
volatile bools are primative. Use ManualResetEvent instead.

What do you have to say?

(will show your gravatar)
Please add 8 and 7 and type the answer here: