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.