C # multi-thread development 4: The Abort and ResetAbort methods of the thread

Source: Internet
Author: User
Tags thread stop

C # multi-thread development 4: The Abort and ResetAbort methods of the thread

The Abort method can be used to Abort a thread, while the ResetAbort method can be used to cancel the thread stop operation.

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

 

Using System; using System. threading; namespace AbortAndResetabortExp {class Program {static void Main (string [] args) {Thread t = new Thread (DoWork); t. name = ""; t. start (); Thread. sleep (1, 10000); Console. writeLine ("Wukong: eight rings, it's time to get up"); t. abort ();} static void DoWork () {try {while (true) {Console. writeLine (Thread. currentThread. name + ": Call ~~~~~ "); Thread. sleep (1000) ;}} catch (ThreadAbortException e) {Console. writeLine (Thread. currentThread. name + ": It's still early, I have to go to bed again"); Thread. resetAbort () ;}for (int I = 0; I <10; I ++) {Console. writeLine (Thread. currentThread. name + ": Call ~~~~~ "); Thread. Sleep (1000 );}}}}

 

In the instance, the main thread starts the "eight rings" thread to "call" to sleep. After 10 seconds, the main thread suspends the "" thread by calling the Abort method of the "" thread. After the "AB "method is called, The ThreadAbortException exception is triggered, after the "" thread detects this exception, it uses the ResetAbort method to cancel the thread stop operation because it has not slept properly yet.

Shows the execution result of the entire program. The result shows that the thread is still executing after the ResetAbort method is called.

 

Related Article

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.