Export (0) Print
Expand All

ThreadAbortException Class

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

For a list of all members of this type, see ThreadAbortException Members.

System.Object
   System.Exception
      System.SystemException
         System.Threading.ThreadAbortException

[Visual Basic]
<Serializable>
NotInheritable Public Class ThreadAbortException
   Inherits SystemException
[C#]
[Serializable]
public sealed class ThreadAbortException : SystemException
[C++]
[Serializable]
public __gc __sealed class ThreadAbortException : public
   SystemException
[JScript]
public
   Serializable
class ThreadAbortException extends SystemException

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.

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 killing the thread. Since the thread can do an unbounded computation in the finally blocks, you must call the Join method to guarantee that the thread has died. Join is a blocking call that does not return until the thread actually stops executing.

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

Example

[Visual Basic, C#, C++] 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."); 
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;
using namespace System::Security::Permissions;

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

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

[Visual Basic, C#, C++] 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.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Threading

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

ThreadAbortException Members | System.Threading Namespace | Thread | Thread.Abort | Destroying Threads

Show:
© 2014 Microsoft