This documentation is archived and is not being maintained.

Thread.ResetAbort Method

Cancels an Abort requested for the current thread.

[Visual Basic]
Public Shared Sub ResetAbort()
[C#]
public static void ResetAbort();
[C++]
public: static void ResetAbort();
[JScript]
public static function ResetAbort();

Exceptions

Exception Type Condition
ThreadStateException Abort was not invoked on the current thread.
SecurityException The caller does not have the required security permission for the current thread.

Remarks

This method can only be called by code with the proper permissions. For more information see Security Demands.

When a call is made to Abort to terminate a thread, the system throws a ThreadAbortException. ThreadAbortException is a special exception that can be caught by application code, but is rethrown 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.

See ThreadAbortException for an example that demonstrates calling the ResetAbort method.

Example

[Visual Basic, C#, C++] The following code example demonstrates that a second call to Abort causes a thread to terminate inside its finally block if ResetAbort was called by the thread in response to the first call to Abort. If ResetAbort was not called, the second call to Abort is ignored.

[Visual Basic] 
Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class Test

    Shared Sub Main()
    
        Dim firstThread  As Thread = New Thread(AddressOf Test.TestMethod)
        Dim secondThread As Thread = New Thread(AddressOf Test.TestMethod)
        firstThread.Name  = "First "
        secondThread.Name = "Second"
        firstThread.Start()
        secondThread.Start()
        Thread.Sleep(2000)

        ' Abort both threads.
        Console.WriteLine(vbCrLf & "Main aborting both threads.")
        firstThread.Abort()
        secondThread.Abort()
        Thread.Sleep(2000)

        ' Call Abort a second time if the threads have not aborted.
        If (firstThread.ThreadState And _
            (ThreadState.Aborted Or ThreadState.Stopped)) = 0 Then
        
            Console.WriteLine(vbCrLf & "Main aborting first thread." & vbCrLf)
            firstThread.Abort()
        End If
        If (secondThread.ThreadState And _
            (ThreadState.Aborted Or ThreadState.Stopped)) = 0 Then
        
            Console.WriteLine(vbCrLf & "Main aborting second thread." & vbCrLf)
            secondThread.Abort()
        End If

        ' Wait for the threads to terminate.
        firstThread.Join()
        Console.WriteLine(vbCrLf & "First  thread terminated.")
        If secondThread.Join(2000) Then
            Console.WriteLine(vbCrLf & "Second thread terminated.")
        Else
            Console.WriteLine( _
                vbCrLf & "Join timed out - second thread still running.")
        End If
        Console.WriteLine(vbCrLf & "Main exiting.")

    End Sub

    Shared Sub TestMethod()
        Dim threadName As String = Thread.CurrentThread.Name
        Try
            While True
                Console.WriteLine("{0} thread running.", threadName)
                Thread.Sleep(1000)
            End While
        Catch ex As ThreadAbortException
        
            ' Only the first thread calls ResetAbort.
            If(threadName = "First ") Then
                Thread.ResetAbort()
                Console.WriteLine(vbCrLf & "First  thread calling ResetAbort.")
            Else
                Console.WriteLine(vbCrLf & "Second thread not calling ResetAbort.")
            End If
        Finally
            While True
                Console.WriteLine("{0} thread running in " & _
                    "Finally block.", threadName)
                Thread.Sleep(1000)
            End While
        End Try
    End Sub

End Class

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

class Test
{
    public static void Main()
    {
        Thread firstThread  = new Thread(new ThreadStart(TestMethod));
        Thread secondThread = new Thread(new ThreadStart(TestMethod));
        firstThread.Name  = "First ";
        secondThread.Name = "Second";
        firstThread.Start();
        secondThread.Start();
        Thread.Sleep(2000);

        // Abort both threads.
        Console.WriteLine("\nMain aborting both threads.");
        firstThread.Abort();
        secondThread.Abort();
        Thread.Sleep(2000);

        // Call Abort a second time if the threads have not aborted.
        if((firstThread.ThreadState & 
            (ThreadState.Aborted | ThreadState.Stopped)) == 0)
        {
            Console.WriteLine("\nMain aborting first thread.\n");
            firstThread.Abort();
        }
        if((secondThread.ThreadState & 
            (ThreadState.Aborted | ThreadState.Stopped)) == 0)
        {
            Console.WriteLine("\nMain aborting second thread.\n");
            secondThread.Abort();
        }

        // Wait for the threads to terminate.
        firstThread.Join();
        Console.WriteLine("\nFirst  thread terminated.");
        if(secondThread.Join(2000))
        {
            Console.WriteLine("\nSecond thread terminated.");
        }
        else
        {
            Console.WriteLine(
                "\nJoin timed out - second thread still running.");
        }

        Console.WriteLine("\nMain exiting.");
    }

    static void TestMethod()
    {
        string threadName = Thread.CurrentThread.Name;
        try
        {
            while(true)
            {
                Console.WriteLine("{0} thread running.", threadName);
                Thread.Sleep(1000);
            }
        }
        catch(ThreadAbortException e)
        {
            // Only the first thread calls ResetAbort.
            if(threadName == "First ")
            {
                Thread.ResetAbort();
            }
            Console.WriteLine("\n{0} thread {1}calling ResetAbort.", 
                threadName, threadName == "First " ? "" : "not ");
        }
        finally
        {
            while(true)
            {
                Console.WriteLine("{0} thread running in " +
                    "finally block.", threadName);
                Thread.Sleep(1000);
            }
        }
    }
}

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

__gc class Test
{
    Test() {}
public:
    static void TestMethod()
    {
        String* threadName = Thread::CurrentThread->Name;
        try
        {
            while(true)
            {
                Console::WriteLine(S"{0} thread running.", 
                    threadName);
                Thread::Sleep(1000);
            }
        }
        catch(ThreadAbortException* e)
        {
            // Only the first thread calls ResetAbort.
            if(threadName == S"First ")
            {
                Thread::ResetAbort();
            }
            Console::WriteLine(S"\n{0} thread {1}calling ResetAbort.",
                threadName, threadName == S"First " ? S"" : S"not ");
        }
        __finally
        {
            while(true)
            {
                Console::WriteLine(S"{0} thread running in "
                    S"finally block.", threadName);
                Thread::Sleep(1000);
            }
        }
    }
};

void main()
{
    Thread* firstThread  = 
        new Thread(new ThreadStart(0, &Test::TestMethod));
    Thread* secondThread = 
        new Thread(new ThreadStart(0, &Test::TestMethod));
    firstThread->Name  = S"First ";
    secondThread->Name = S"Second";
    firstThread->Start();
    secondThread->Start();
    Thread::Sleep(2000);

    // Abort both threads.
    Console::WriteLine(S"\nMain aborting both threads.");
    firstThread->Abort();
    secondThread->Abort();
    Thread::Sleep(2000);

    // Call Abort a second time if the threads have not aborted.
    if((firstThread->ThreadState & 
        (ThreadState::Aborted | ThreadState::Stopped)) == 0)
    {
        Console::WriteLine(S"\nMain aborting first thread.\n");
        firstThread->Abort();
    }
    if((secondThread->ThreadState & 
        (ThreadState::Aborted | ThreadState::Stopped)) == 0)
    {
        Console::WriteLine(S"\nMain aborting second thread.\n");
        secondThread->Abort();
    }

    // Wait for the threads to terminate.
    firstThread->Join();
    Console::WriteLine(S"\nFirst  thread terminated.");
    if(secondThread->Join(2000))
    {
        Console::WriteLine(S"\nSecond thread terminated.");
    }
    else
    {
        Console::WriteLine(
            S"\nJoin timed out - second thread still running.");
    }

    Console::WriteLine(S"\nMain 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 | Destroying Threads | Thread States

Show: