C # Thread Termination and Thread.Abort ()

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

Why not ALWAYS?

In what cases does it not interrupt the flow?

Is there any other way to stop threads?

+75
multithreading c #
Feb 12 2018-10-12
source share
10 answers

Thread.Abort() a ThreadAbortException into the thread. A thread can cancel a request by calling Thread.ResetAbort() . In addition, there are certain pieces of code, such as the finally block, that will be executed before the exception is processed. If for some reason the thread is stuck in such a block, an exception will never be raised in the thread.

Since the caller has very little control over the state of the stream when calling Abort() , this is usually not recommended. Pass the message to the thread requesting completion.

+51
Feb 12 '10 at 13:22
source share

In what cases does it not interrupt the flow?

This question is a duplicate.

What is wrong with Thread.Abort ()

Is there any other way to stop threads?

Yes. Your problem is that you should never run a thread that you cannot politely say, and it stops in a timely manner. If you are in a situation where you need to start a thread that can be (1) difficult to stop, (2) a mistake or the worst of all (3) hostile to the user, then the right thing is to make a new process, start the thread in a new process, and then complete the process when you want the thread to drop. The only thing that can guarantee the safe termination of an incompatible thread is the operating system, which removes the entire process.

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

Using a lock statement in a loop in C #

The corresponding bit is the bit at the end where I discuss what considerations concern how long you should wait for the thread to kill itself before you stop it.

+46
Feb 12 '10 at 16:06
source share

Why not ALWAYS? In what cases does he not construct a thread?

To begin with, a thread can catch a ThreadAbortException and discard its own termination. Or he can perform calculations that are performed forever while you try to interrupt him. Because of this, the runtime cannot guarantee that the thread will always terminate after a request.

ThreadAbortException has more:

When you call the Abort method to kill a thread, the common runtime throws a ThreadAbortException. ThreadAbortException is a special exception that can be caught, but it will be automatically raised again at the end of the catch block. When this exception occurs, the runtime executes all finally blocks until the thread ends. Because a thread can perform unlimited computation in finally blocks or call Thread.ResetAbort() to cancel an interrupt, there is no guarantee that the thread will never end.

You do not need to Abort() stream manually. The CLR will do all the dirty work for you if you just let the method in the thread return; which normally terminates the thread.

+15
Feb 12 2018-10-12
source share

FileStream.Read() to the named pipe that is currently receiving nothing (reads call blocks while waiting for incoming data) does not respond to Thread.Abort() . It stays inside the Read() call.

+6
Sep 22 2018-11-11T00:
source share

What if the thread holds the lock and is interrupted / killed? Resources remain stuck

It works great when interrupting itself when a thread is called, and not by another thread. Interruption, forcibly terminates the affected thread, even if it has not completed its task and provided an opportunity to clean resources

MSDN link




see below: Recommended flow control methods

+5
Feb 12 2018-10-12
source share

ThreadAborts will not happen inside the finally block or between BeginCriticalRegion and EndCriticalRegion

+4
Feb 12 2018-10-12
source share

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

+3
Feb 12 2018-10-12
source share

I cannot interrupt a thread that is stuck in a loop:

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

However, I can interrupt the thread if it sleeps during the loop:

 //mortal Thread th2 = new Thread(() => { while (true) { Thread.Sleep(1000); }}); 
+2
Nov 24 '15 at 8:10
source share

OT: For a comprehensive, linguistic agnostic, dubiously useful and damn funny, take concurrency, see Verity Stob !

0
Feb 12 2018-10-12
source share

I have had cases where the thread was too busy to hear the call to Abort (), which usually causes the ThreadAbortingException to be thrown into my code.

-one
Feb 12 2018-10-12
source share



All Articles