Mono Class Library: System.Threading NamespaceSystem.Threading.ThreadAbortException Class |
See Also: ThreadAbortException Members
System.Object
System.Exception
System.SystemException
System.Threading.ThreadAbortException
|
All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.
Instances of this exception type can only be created by the system.
When a call is made to Thread.Abort(object) to terminate a thread, the system throws a System.Threading.ThreadAbortException in the target thread. System.Threading.ThreadAbortException is a special exception that can be caught by application code, but is rethrown at the end of the catch block unless Thread.ResetAbort is called. When the ThreadAbortException exception is raised, the system executes any finally blocks for the target thread. The finally blocks are executed even if Thread.ResetAbort is called. If the abort is successful, the target thread is left in the ThreadState.Stopped and ThreadState.Aborted states.
[Edit]
The following example demonstrates aborting a thread. The thread that receives the System.Threading.ThreadAbortException uses the Thread.ResetAbort method to cancel the abort request and continue executing.
C# Example using System; using System.Threading; using System.Security.Permissions; public class ThreadWork { public static void DoWork() { try { for (int i=0; i<100; i++) { Console.WriteLine("Thread - working."); Thread.Sleep(100); } } catch (ThreadAbortException e) { Console.WriteLine("Thread - caught ThreadAbortException - resetting."); Thread.ResetAbort(); } Console.WriteLine("Thread - still alive and working."); Thread.Sleep(1000); Console.WriteLine("Thread - finished working."); } } class ThreadAbortTest{ public static void Main() { ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); Thread myThread = new Thread(myThreadDelegate); myThread.Start(); Thread.Sleep(100); Console.WriteLine("Main - aborting my thread."); myThread.Abort(); myThread.Join(); Console.WriteLine("Main ending."); } }The output is
Thread - working.
Main - aborting my thread.
Thread - caught ThreadAbortException - resetting.
Thread - still alive and working.
Thread - finished working.
Main ending.
Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0