Skip to main content
.NET Framework Class Library
ThreadAbortException Class

The exception that is thrown when a call is made to the Abort method. This class cannot be inherited.

Inheritance Hierarchy
SystemObject
   SystemException
     SystemSystemException
      System.ThreadingThreadAbortException

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/he1x3cf1_en-us_VS_110_3_0_0_0_0%]> _
<[%$TOPIC/he1x3cf1_en-us_VS_110_3_0_0_0_1%](True)> _
Public NotInheritable Class ThreadAbortException _
	Inherits [%$TOPIC/he1x3cf1_en-us_VS_110_3_0_0_0_2%]
[[%$TOPIC/he1x3cf1_en-us_VS_110_3_0_1_0_0%]]
[[%$TOPIC/he1x3cf1_en-us_VS_110_3_0_1_0_1%](true)]
public sealed class ThreadAbortException : [%$TOPIC/he1x3cf1_en-us_VS_110_3_0_1_0_2%]
[[%$TOPIC/he1x3cf1_en-us_VS_110_3_0_2_0_0%]]
[[%$TOPIC/he1x3cf1_en-us_VS_110_3_0_2_0_1%](true)]
public ref class ThreadAbortException sealed : public [%$TOPIC/he1x3cf1_en-us_VS_110_3_0_2_0_2%]
[<[%$TOPIC/he1x3cf1_en-us_VS_110_3_0_3_0_0%]>]
[<[%$TOPIC/he1x3cf1_en-us_VS_110_3_0_3_0_1%]>]
[<[%$TOPIC/he1x3cf1_en-us_VS_110_3_0_3_0_2%](true)>]
type ThreadAbortException =  
    class 
        inherit [%$TOPIC/he1x3cf1_en-us_VS_110_3_0_3_0_3%] 
    end

The ThreadAbortException type exposes the following members.

Properties
  NameDescription
Public property DataGets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)
Public property Supported by the XNA Framework ExceptionStateGets an object that contains application-specific information related to the thread abort.
Public property HelpLinkGets or sets a link to the help file associated with this exception. (Inherited from Exception.)
Public property HResultGets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)
Public property Supported by the XNA Framework InnerExceptionGets the Exception instance that caused the current exception. (Inherited from Exception.)
Public property Supported by the XNA Framework MessageGets a message that describes the current exception. (Inherited from Exception.)
Public property SourceGets or sets the name of the application or the object that causes the error. (Inherited from Exception.)
Public property Supported by the XNA Framework StackTraceGets a string representation of the immediate frames on the call stack. (Inherited from Exception.)
Public property TargetSiteGets the method that throws the current exception. (Inherited from Exception.)
Top
Methods
  NameDescription
Public method Supported by the XNA Framework Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method Supported by the XNA Framework GetBaseExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.)
Public method Supported by the XNA Framework GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public method GetObjectDataWhen overridden in a derived class, sets the SerializationInfo with information about the exception. (Inherited from Exception.)
Public method Supported by the XNA Framework GetTypeGets the runtime type of the current instance. (Inherited from Exception.)

In XNA Framework 3.0, this member is inherited from Object GetType.
Public method Supported by the XNA Framework ToStringCreates and returns a string representation of the current exception. (Inherited from Exception.)
Top
Remarks

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block. When this exception is raised, the runtime executes all the finally blocks before ending the thread. Because the thread can do an unbounded computation in the finally blocks or call ThreadResetAbort to cancel the abort, there is no guarantee that the thread will ever end. If you want to wait until the aborted thread has ended, you can call the ThreadJoin method. Join is a blocking call that does not return until the thread actually stops executing.

NoteNote

When the common language runtime (CLR) stops background threads after all foreground threads in a managed executable have ended, it does not use ThreadAbort. Therefore, you cannot use ThreadAbortException to detect when background threads are being terminated by the CLR.

ThreadAbortException uses the HRESULT COR_E_THREADABORTED, which has the value 0x80131530.

NoteNote

The value of the inherited Data property is always .

Examples

The following example demonstrates aborting a thread. The thread that receives the ThreadAbortException uses the ResetAbort method to cancel the abort request and continue executing.

Imports System
Imports System.Threading
Imports System.Security.Permissions


Public Class ThreadWork
   Public Shared Sub DoWork()
      Try 
         Dim i As Integer 
         For i = 0 To 99
            Console.WriteLine("Thread - working.")
            Thread.Sleep(100)
         Next i
      Catch e As ThreadAbortException
         Console.WriteLine("Thread - caught ThreadAbortException - resetting.")
         Console.WriteLine("Exception message: {0}", e.Message)
         Thread.ResetAbort()
      End Try
      Console.WriteLine("Thread - still alive and working.")
      Thread.Sleep(1000)
      Console.WriteLine("Thread - finished working.")
   End Sub 'DoWork
End Class 'ThreadWork


Class ThreadAbortTest
   Public Shared Sub Main()
      Dim myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork)
      Dim myThread As New Thread(myThreadDelegate)
      myThread.Start()
      Thread.Sleep(100)
      Console.WriteLine("Main - aborting my thread.")
      myThread.Abort()
      myThread.Join()
      Console.WriteLine("Main ending.")
   End Sub 'Main
End Class 'ThreadAbortTest
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.");
            Console.WriteLine("Exception message: {0}", e.Message);
            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."); 
    }
}
using namespace System;
using namespace System::Threading;
using namespace System::Security::Permissions;
ref 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." );
         Console::WriteLine( "Exception message: {0}", e->Message );
         Thread::ResetAbort();
      }

      Console::WriteLine( "Thread - still alive and working." );
      Thread::Sleep( 1000 );
      Console::WriteLine( "Thread - finished working." );
   }

};

int main()
{
   ThreadStart^ myThreadDelegate = gcnew ThreadStart( ThreadWork::DoWork );
   Thread^ myThread = gcnew Thread( myThreadDelegate );
   myThread->Start();
   Thread::Sleep( 100 );
   Console::WriteLine( "Main - aborting my thread." );
   myThread->Abort();
   myThread->Join();
   Console::WriteLine( "Main ending." );
}

This code produces the following output:

 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.
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.