Does the C# Thread.Abort method really stop the thread?

 

Everyone knows that in C#, we can use the Thread.Start method to start a thread. When we want to stop the executing thread, we can use the Thread.Abort method to forcefully stop the executing thread, but please note that you are sure to call Thread Does the thread stop immediately after the .Abort method? The answer is: no!

Let's explain how the Abort method works. Because the common language runtime manages all managed threads, it can also throw exceptions in each thread. The Abort method can throw a ThreadAbortException in the target thread and cause the termination of the target thread. However, after the Abort method is called, the target thread may not terminate immediately. Because as long as the target thread is calling unmanaged code and has not returned, the thread will not terminate immediately. And if the target thread is calling unmanaged code and falls into an infinite loop, the target thread will not terminate at all. However, this situation is only a special case. More often, the target thread is calling managed code. Once Abort is called, the thread is immediately terminated.

In fact, when a thread is running, we can read its state through the Thread.ThreadState property, and the running thread state is ThreadState.Running. Then if we want to forcibly stop the executing thread, we will call the Thread.Abort method, but what the Thread.Abort method does is throw a ThreadAbortException exception on the thread, and then set the thread state to ThreadState.AbortRequested, MSDN The explanation for the AbortRequested state is: the Thread.Abort method has been called on the thread, but the thread has not yet received the pending System.Threading.ThreadAbortException that attempts to terminate it , which means that the thread is in the ThreadState.AbortRequested state, indicating that it is about to end but still There is no real end. But the Thread.Abort method sets the thread state to ThreadState.AbortRequested and returns immediately, and the state after the thread really ends should be ThreadState.Aborted, so be sure to remember to check the loop after calling the Thread.Abort method The value of the Thread.ThreadState property or the Thread.Join method is called to ensure that the terminated thread has really stopped. Only when the Thread.ThreadState property is Aborted or the Thread.Join method returns, it means that the thread is really over.

Below I will write a sample code to illustrate how to ensure that the code will continue to execute after the thread stops after calling the Thread.Abort method

 

Copy code

var thread = new Thread( 
    new ThreadStart( 
        () => 
            { 
                while (true) 
                { 
                    //The thread will loop infinitely and will not end by itself 
                    Thread.Sleep(100); 
                } 
            })); 

thread.IsBackground = true; 
thread.Start();//Start thread 

thread.Abort();//Call the Thread.Abort method to try to forcibly terminate the thread thread 

//Thread thread may not be terminated immediately after calling the Thread.Abort method above, so we are in Here is a loop to check to see if the thread has really stopped. In fact, you can also use the Thread.Join method here to wait for the thread to terminate. The Thread.Join method does the same thing as the loop effect we wrote here, blocking the main thread until the thread thread terminates 
while (thread.ThreadState !=ThreadState.Aborted) 
{ 
    //When the Abort method is called, if the state of the thread thread is not Aborted, the main thread will continue to loop here until the state of the thread thread becomes Aborted
    Thread.Sleep(100); 
} 

//When we break out of the above loop, it means that the thread thread we started has completely terminated

Guess you like

Origin blog.csdn.net/u014780302/article/details/100775327