C# Thread Termination and Thread.Abort()

C#.NetMultithreading

C# Problem Overview


In MSDN, the description of the Thread.Abort() method says: "Calling this method usually terminates the thread."

Why not ALWAYS?

In which cases it doesn't terminate the thread?

Are there any other possibility to terminate threads?

C# Solutions


Solution 1 - C#

Thread.Abort() injects a ThreadAbortException on the thread. The thread may cancel the request by calling Thread.ResetAbort(). Also, there are certain code parts, such as finally block that will execute before the exception is handled. If for some reason the thread is stuck in such a block the exception will never be raised on the thread.

As the caller has very little control over the state of the thread when calling Abort(), it is generally not advisable to do so. Pass a message to the thread requesting termination instead.

Solution 2 - C#

> In which cases it doesn't terminate the thread?

This question is a duplicate.

https://stackoverflow.com/questions/1559255/whats-wrong-with-using-thread-abort/1560567#1560567

> Are there any other posibility to terminate threads?

Yes. Your problem is that you should never start up a thread that you cannot tell politely to stop, and it stops in a timely manner. If you are in a situation where you have to start up a thread that might be (1) hard to stop, (2) buggy, or worst of all (3) hostile to the user, then the right thing to do is to make a new process, start the thread in the new process, and then terminate the process when you want the thread to go down. The only thing that can guarantee safe termination of an uncooperative thread is the operating system taking down its entire process.

See my excessively long answer to this question for more details:

https://stackoverflow.com/questions/2113261/using-lock-statement-within-a-loop-in-c

The relevant bit is the bit at the end where I discuss what the considerations are regarding how long you should wait for a thread to kill itself before you abort it.

Solution 3 - C#

> Why not ALWAYS? > In which cases it doesn't termenate the thread?

For starters, a thread may catch a ThreadAbortException and cancel its own termination. Or it could perform a computation that takes forever while you're trying to abort it. Because of this, the runtime can't guarantee that the thread will always terminate after you ask it to.

ThreadAbortException has more:

> When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block. When this exception is raised, the runtime executes all the finally blocks before ending the thread. Since the thread can do an unbounded computation in the finally blocks, or call Thread.ResetAbort() to cancel the abort, there is no guarantee that the thread will ever end.

You don't need to Abort() a thread manually. The CLR will do all of the dirty work for you if you simply let the method in the thread return; that will end the thread normally.

Solution 4 - C#

FileStream.Read() to a named pipe that is currently not receiving anything (read call blocks while waiting for incoming data) will not respond to Thread.Abort(). It remains inside the Read() call.

Solution 5 - C#

What if a thread is holding a lock and is aborted / killed ? Resources remain stuck

> It works fine when when a thread calls > abort itself but not by other thread. > Abort, forcefully terminates the > affected thread even if it has not > completed its task and provides no > opportunity for the cleanup of > resources

reference MSDN


see: Managed Threading Best Practices

Solution 6 - C#

I can't seem to abort a thread that is stuck in a loop:

//immortal
Thread th1 = new Thread(() => { while (true) {}});

I can however abort the thread if sleeps during the loop:

//mortal
Thread th2 = new Thread(() => { while (true) { Thread.Sleep(1000); }});

Solution 7 - C#

ThreadAborts will not occur inside a finally block or between BeginCriticalRegion and EndCriticalRegion

Solution 8 - C#

Because you can catch the ThreadAbortException and call Thread.ResetAbort inside the handler.

Solution 9 - C#

OT: For a comprehensive, language-agnostic, questionably useful and darned funny take on concurrency, see Verity Stob!

Solution 10 - C#

I've had cases where the thread has been too busy to hear the Abort() call, which usually results in a ThreadAbortingException being thrown to my code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser101375View Question on Stackoverflow
Solution 1 - C#Brian RasmussenView Answer on Stackoverflow
Solution 2 - C#Eric LippertView Answer on Stackoverflow
Solution 3 - C#John FeminellaView Answer on Stackoverflow
Solution 4 - C#jovainView Answer on Stackoverflow
Solution 5 - C#AsadView Answer on Stackoverflow
Solution 6 - C#ColinView Answer on Stackoverflow
Solution 7 - C#Rob Fonseca-EnsorView Answer on Stackoverflow
Solution 8 - C#erikkallenView Answer on Stackoverflow
Solution 9 - C#Pontus GaggeView Answer on Stackoverflow
Solution 10 - C#Andy ShellamView Answer on Stackoverflow