Thread aborted-"thread was being aborted"

Source: Internet
Author: User

Have you ever encountered this exception? Why is this exception?

The following code snippet comes from msdn, Which is descriptive.

 

To put it simply, when the process still wants to execute, it finds that it has already called the abort method. since the thread itself has been suspended, it cannot be executed, so the exception is lost.

 

The following code is from msdn to explain the problem:

The following example demonstrates aborting a thread. The thread that corresponds es the threadabortexception uses the resetabort method to cancel the abort request and continue executing.

   1: using System;

   2: using System.Threading;

   3: using System.Security.Permissions;

   4:  

   5: public class ThreadWork {

   6:     public static void DoWork() {

   7:         try {

   8:             for(int i=0; i<100; i++) {

   9:                 Console.WriteLine("Thread - working."); 

  10:                 Thread.Sleep(100);

  11:             }

  12:         }

  13:         catch(ThreadAbortException e) {

  14:             Console.WriteLine("Thread - caught ThreadAbortException - resetting.");

  15:             Console.WriteLine("Exception message: {0}", e.Message);

  16:             Thread.ResetAbort();

  17:         }

  18:         Console.WriteLine("Thread - still alive and working."); 

  19:         Thread.Sleep(1000);

  20:         Console.WriteLine("Thread - finished working.");

  21:     }

  22: }

  23:  

  24: class ThreadAbortTest {

  25:     public static void Main() {

  26:         ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);

  27:         Thread myThread = new Thread(myThreadDelegate);

  28:         myThread.Start();

  29:         Thread.Sleep(100);

  30:         Console.WriteLine("Main - aborting my thread.");

  31:         myThread.Abort();

  32:         myThread.Join();

  33:         Console.WriteLine("Main ending."); 

  34:     }

  35: }

 

The output result is as follows:

Thread-working.

Main-aborting my thread.

Thread-caught threadabortexception-resetting.

Exception message: thread was being aborted.

Thread-still alive and working.

Thread-finished working.

Main ending.

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.