Export (0) Print
Expand All

Thread.Abort Method (Object)

.NET Framework 1.1

Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread while also providing exception information about the thread termination. Calling this method usually terminates the thread.

[Visual Basic]
Overloads Public Sub Abort( _
   ByVal stateInfo As Object _
)
[C#]
public void Abort(
 object stateInfo
);
[C++]
public: void Abort(
 Object* stateInfo
);
[JScript]
public function Abort(
   stateInfo : Object
);

Parameters

stateInfo
An object that contains application-specific information, such as state, which can be used by the thread being aborted.

Exceptions

Exception Type Condition
SecurityException The caller does not have the required permission.

Remarks

When this method is invoked on a thread, the system throws a ThreadAbortException in the thread to abort it. ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread. Unexecuted finally blocks are executed before the thread is aborted.

Note   When a thread calls Abort on itself, the effect is similar to throwing an exception; the ThreadAbortException happens immediately, and the result is predictable. However, if one thread calls Abort on another thread, the abort interrupts whatever code is running. There is a chance the thread could abort while a finally block is running, in which case the finally block is aborted. There is also a chance that a static constructor could be aborted. In rare cases, this might prevent instances of that class from being created in that application domain.

The thread is not guaranteed to abort immediately, or at all. This situation can occur if a thread does an unbounded amount of computation in the finally blocks that are called as part of the abort procedure, thereby indefinitely delaying the abort. To ensure a thread has aborted, invoke a Join method on the thread after calling Abort.

If Abort is called on a thread that has not been started, the thread will abort when Start is called. If Abort is called on a thread that has been suspended, the thread is resumed and then aborted. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted. If, while executing unmanaged code, a thread ignores a ThreadAbortException, the system re-throws the ThreadAbortException when the thread begins executing managed code.

If two calls to Abort come at the same time, it is possible that one call sets the state information, and the other call executes the Abort. However, an application cannot distinguish this.

After Abort is invoked on a thread, the state of the thread includes AbortRequested. After the thread has terminated as a result of a successful call to Abort, the state of the thread is changed to Stopped. With sufficient permissions, a thread that is the target of an Abort can cancel the abort using the ResetAbort method. For an example that demonstrates calling the ResetAbort method, see the ThreadAbortException class.

Example

[Visual Basic, C#, C++] The following code example shows how to pass information to a thread that is being aborted.

[Visual Basic] 
Imports System
Imports System.Threading

Public Class Test

    Shared Sub Main()
        Dim newThread As New Thread(AddressOf TestMethod)
        newThread.Start()
        Thread.Sleep(1000)

        ' Abort newThread.
        Console.WriteLine("Main aborting new thread.")
        newThread.Abort("Information from Main.")

        ' Wait for the thread to terminate.
        newThread.Join()
        Console.WriteLine("New thread terminated - Main exiting.")
    End Sub

    Shared Sub TestMethod()
        Try
            While True
                Console.WriteLine("New thread running.")
                Thread.Sleep(1000)
            End While
        Catch abortException As ThreadAbortException
            Console.WriteLine( _
                CType(abortException.ExceptionState, String))
        End Try
    End Sub

End Class

[C#] 
using System;
using System.Threading;

class Test
{
    public static void Main()
    {
        Thread newThread  = new Thread(new ThreadStart(TestMethod));
        newThread.Start();
        Thread.Sleep(1000);

        // Abort newThread.
        Console.WriteLine("Main aborting new thread.");
        newThread.Abort("Information from Main.");

        // Wait for the thread to terminate.
        newThread.Join();
        Console.WriteLine("New thread terminated - Main exiting.");
    }

    static void TestMethod()
    {
        try
        {
            while(true)
            {
                Console.WriteLine("New thread running.");
                Thread.Sleep(1000);
            }
        }
        catch(ThreadAbortException abortException)
        {
            Console.WriteLine((string)abortException.ExceptionState);
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

__gc class Test
{
    Test() {}
public:
    static void TestMethod()
    {
        try
        {
            while(true)
            {
                Console::WriteLine(S"New thread running.");
                Thread::Sleep(1000);
            }
        }
        catch(ThreadAbortException* abortException)
        {
            Console::WriteLine(dynamic_cast<String*>(
                abortException->ExceptionState));
        }
    }
};

    void main()
    {
        Thread* newThread = 
            new Thread(new ThreadStart(0, &Test::TestMethod));
        newThread->Start();
        Thread::Sleep(1000);

        // Abort newThread.
        Console::WriteLine(S"Main aborting new thread.");
        newThread->Abort(S"Information from main.");

        // Wait for the thread to terminate.
        newThread->Join();
        Console::WriteLine(S"New thread terminated - main exiting.");
    }

[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

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Thread Class | Thread Members | System.Threading Namespace | Thread.Abort Overload List | ThreadAbortException | Aborted | AbortRequested | Threads and Threading | Using Threads and Threading | Destroying Threads

Show:
© 2015 Microsoft