Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ThreadAbortException Class

Updated: November 2007

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

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class ThreadAbortException _
    Inherits SystemException
Visual Basic (Usage)
Dim instance As ThreadAbortException
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class ThreadAbortException : SystemException
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class ThreadAbortException sealed : public SystemException
J#
/** @attribute SerializableAttribute */ 
/** @attribute ComVisibleAttribute(true) */
public final class ThreadAbortException extends SystemException
JScript
public final class ThreadAbortException extends SystemException

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. Since the thread can do an unbounded computation in the finally blocks, or call Thread..::.ResetAbort 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 Thread..::.Join method. Join is a blocking call that does not return until the thread actually stops executing.

Note:

When the common language runtime (CLR) stops background threads, after all foreground threads in a managed executable have ended, it does not use Thread..::.Abort. 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.

Note:

The value of the inherited Data property is always nullNothingnullptra null reference (Nothing in Visual Basic).

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.

Visual Basic
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

C#
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."); 
    }
}

Visual C++
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." );
}


J#
import System.*;
import System.Threading.*;
import System.Security.Permissions.*;

public class ThreadWork
{
    public static void DoWork()
    {
        try {
            for (int i = 0; i < 100; i++) {
                Console.WriteLine("Thread - working.");
                System.Threading.Thread.Sleep(100);
            }
        }
        catch (ThreadAbortException e) {
            Console.WriteLine("Thread - caught ThreadAbortException"
                + " - resetting.");
            Console.WriteLine("Exception message: {0}", e.get_Message());
            System.Threading.Thread.ResetAbort();
        }
        Console.WriteLine("Thread - still alive and working.");
        System.Threading.Thread.Sleep(1000);
        Console.WriteLine("Thread - finished working.");
    } //DoWork
} //ThreadWork

class ThreadAbortTest
{
    public static void main(String[] args)
    {
        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
        System.Threading.Thread myThread =
            new System.Threading.Thread(myThreadDelegate);
        myThread.Start();
        System.Threading.Thread.Sleep(100);
        Console.WriteLine("main - aborting my thread.");
        myThread.Abort();
        myThread.Join();
        Console.WriteLine("main ending.");
    } //main
} //ThreadAbortTest

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.
System..::.Object
  System..::.Exception
    System..::.SystemException
      System.Threading..::.ThreadAbortException
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Thread.Abort should be avoided.      Peter Ritchie   |   Edit   |  
There's many reasons not to use Thread.Abort and ThreadAbortException
  • On certain platforms (like x64 and IA64) the abort can occur before Monitor.Enter and a try block (even with lock/SyncLock), leaving the monitor orphaned.
  • The ThreadAbortException can occur in 3rd party code not written to handle thread abort.
  • The thread can be aborted while processing a finally block in .NET 1.x
  • Uses exceptions for normal control flow logic.
  • Asynchronous exception can interrupt modification of shard state or resources, leaving them corrupted.

For more detail see:

Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker