Thread.Abort Method
Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
Overload List
Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
[Visual Basic] Overloads Public Sub Abort()
[C#] public void Abort();
[C++] public: void Abort();
[JScript] public function Abort();
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(Object)
[C#] public void Abort(object);
[C++] public: void Abort(Object*);
[JScript] public function Abort(Object);
Example
[Visual Basic, C#, C++] The following code example shows how to pass information to a thread that is being aborted.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Abort. For other examples that might be available, see the individual overload topics.
[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
in the upper-left corner of the page.