C # multithreaded Development 4: Thread abort and ResetAbort methods

Source: Internet
Author: User

You can abort a thread by using the Abort method, and you can cancel the abort of the thread by using the ResetAbort method .

The following example demonstrates the use of the abort and ResetAbort methods .

<span style= "FONT-SIZE:14PX;" >using system;using System.threading;namespace abortandresetabortexp{class Program {static void Main (str            Ing[] args) {thread t = new Thread (DoWork);            T.name = "Eight commandments";            T.start ();            Thread.Sleep (10000);            Console.WriteLine ("Goku: Eight commandments, it's time to get Up");        T.abort ();                    } static void DoWork () {try {while (true) {                    Console.WriteLine (Thread.CurrentThread.Name + ": Whistling ~~~~~");                Thread.Sleep (1000); }} catch (ThreadAbortException e) {Console.WriteLine (Thread.currentthrea                D.name + ": Early, I have to sleep again");            Thread.resetabort (); } for (int i = 0; i < i++) {Console.WriteLine (Thread.CurrentThread.Name + ": Call                Call ~~~~~ ");            Thread.Sleep (1000);   }        } }}</span> 

instance, the main thread initiates the "eight commandments" threads, making it "whirring" to sleep. Ten seconds, the main thread calls the "eight commandments" thread Abort method aborts the "eight commandments" thread, and the "Eight commandments" thread Abort is triggered when the method is called ThreadAbortException exception, after the "eight commandments" thread catches the exception, use the ResetAbort method to cancel the abort of the thread, because he hasn't slept enough yet.

As shown in the execution of the entire program, it can be seen from the results that the thread is still executing after the ResetAbort method is called.


C # multithreaded Development 4: Thread abort and ResetAbort methods

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.