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

Source: Internet
Author: User

As we all know in C #, we can use Thread.Start method to start a thread, you can use the Thread.Abort method to force a thread to stop executing when we want to stop executing the thread, but be aware that you are sure that the thread stopped immediately after you called the Thread.Abort method? The answer is: No!

Let's try to explain how the Abort method works. Because the common language runtime manages all managed threads, it can also throw exceptions within each thread. The Abort method can throw a ThreadAbortException exception in the target thread, causing the target thread to terminate. However, after the Abort method is called, the target thread may not terminate immediately. This thread will not terminate immediately, as long as the target thread is calling unmanaged code and has not returned. if the target thread is calling unmanaged code and is trapped in a dead loop, the target thread will not terminate at all. However, this is a case in which the target thread calls the managed code and the thread terminates immediately once abort is called.

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. And then if we want to force the execution of the thread to stop, The Thread.Abort method is called, but what the Thread.Abort method does is throw a ThreadAbortException exception on the thread and then set the state of the threads to Threadstate.abortrequested,msdn to ABORTR The equested state is interpreted as having called the Thread.Abort method on the thread, but the thread has not received a pending System.Threading.ThreadAbortException that attempted to terminate it. This means that the thread is in the threadstate.abortrequested state and is about to end but not really finished. However, the Thread.Abort method returns the state of the thread after it is threadstate.abortrequested, and the thread actually finishes after the state is threadstate.aborted, so be aware that after calling the Thread.Abort method, remember to You have to loop through the value of the Thread.threadstate property or call the Thread.Join method to ensure that the terminated thread has actually stopped, only if the Thread.threadstate property is returned by the aborted or Thread.Join method, which means the thread is really over.

I'll write a sample code below to show how the code will continue to execute after calling the Thread.Abort method to ensure that the thread stops.

varThread =NewThread (NewThreadStart (()=            {                 while(true)                {                    //the thread will make an infinite loop and will not end itselfThread.Sleep ( -); })); thread. IsBackground=true; thread. Start ();//Start Threadthread. Abort ();//call the Thread.Abort method to attempt to force termination of thread thread//thread threads do not have to be terminated immediately after calling the Thread.Abort method, so we write a loop here to check to see if thread threads have actually stopped. You can actually use the Thread.Join method here to wait for thread termination, the Thread.Join method does the same as the loop we write here, blocking the main thread until the thread terminates. while(Thread. threadstate!=threadstate.aborted) {    //when the Abort method is called, if the thread's state is not aborted, the main thread is doing the loop here until the thread's state changes to aborted.Thread.Sleep ( -);}//when we jump out of the loop above, it means that the thread that we started has been completely terminated.

Remember, however, that it is not a good practice to use the Thread.Abort method to terminate a thread that is executing, because the Abort method terminates the thread by throwing an exception through the threads, which can cause some unexpected problems. The best way to do this is to add a semaphore to the thread that starts, and when you want to terminate the execution of the thread, change the state of the semaphore, which is the safest way to start the thread when it reads the state of the semaphore and ends the execution of the code itself.

Original link: https://www.cnblogs.com/OpenCoder/p/4587249.html

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.