The exception that is thrown when a call is made to the Abort(Object) method. This class cannot be inherited.
public ref class ThreadAbortException sealed : SystemException
public sealed class ThreadAbortException : SystemException
[System.Serializable]
public sealed class ThreadAbortException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ThreadAbortException : SystemException
type ThreadAbortException = class
inherit SystemException
[<System.Serializable>]
type ThreadAbortException = class
inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ThreadAbortException = class
inherit SystemException
Public NotInheritable Class ThreadAbortException
Inherits SystemException
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.
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.");
}
}
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
End Class
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
End Class
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.
When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException on .NET Framework. 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. Because 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
.NET Core and .NET 5+ only: Even though this type exists in .NET Core and .NET 5+, since Abort is not supported, the common language runtime won't ever throw ThreadAbortException.
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 HRESULT COR_E_THREADABORTED
, which has the value 0x80131530
.
Note
The value of the inherited Data property is always null
.
Gets a collection of key/value pairs that provide additional user-defined information about the exception.
(Inherited from Exception) ExceptionStateGets an object that contains application-specific information related to the thread abort.
HelpLinkGets or sets a link to the help file associated with this exception.
(Inherited from Exception) HResultGets or sets HRESULT, a coded numerical value that is assigned to a specific exception.
(Inherited from Exception) InnerExceptionGets the Exception instance that caused the current exception.
(Inherited from Exception) MessageGets a message that describes the current exception.
(Inherited from Exception) SourceGets or sets the name of the application or the object that causes the error.
(Inherited from Exception) StackTraceGets a string representation of the immediate frames on the call stack.
(Inherited from Exception) TargetSiteGets the method that throws the current exception.
(Inherited from Exception) Methods Events SerializeObjectStateObsolete.
Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.
(Inherited from Exception) See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4