System.Threading.Thread.Abort Method

Raises a System.Threading.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.

Syntax

public void Abort (object stateInfo)

Parameters

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

Exceptions

TypeReason
System.Security.SecurityException Caller does not have System.Security.Permissions.SecurityPermissionFlag.ControlThread security permission for this thread.

Remarks

When this method is invoked on a thread, the system throws a System.Threading.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 Thread.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 System.Threading.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 that a static constructor could be aborted. In rare cases, this might prevent instances of that class from being created in that application domain. In the .NET Framework versions 1.0 and 1.1, there is a chance the thread could abort while a finally block is running, in which case the finally block is aborted.

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 wait until a thread has aborted, you can call the Thread.Join method on the thread after calling the Thread.Abort(object) method, but there is no guarantee that the wait will end.

Note:

The thread that calls Thread.Abort might block if the thread that is being aborted is in a protected region of code, such as a catch block, finally block, or constrained execution region. If the thread that calls Thread.Abort holds a lock that the aborted thread requires, a deadlock can occur.

If Abort is called on a thread that has not been started, the thread will abort when Thread.Start is called. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.

If Abort is called on a thread that has been suspended, a System.Threading.ThreadStateException is thrown in the thread that called erload:System.Threading.Thread.Abort, and ThreadState.AbortRequested is added to the Thread.ThreadState property of the thread being aborted. A System.Threading.ThreadAbortException is not thrown in the suspended thread until Thread.Resume is called.

If Abort is called on a managed thread while it is executing unmanaged code, a ThreadAbortException is not thrown until the thread returns to managed code.

If two calls to Abort come at the same time, it is possible for one call to set the state information and the other call to execute the Abort. However, an application cannot detect this situation.

After Abort is invoked on a thread, the state of the thread includes ThreadState.AbortRequested. After the thread has terminated as a result of a successful call to Abort, the state of the thread is changed to ThreadState.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.

Permissions

TypeReason
!:System.Security.SecurityPermissionRequires permission to control the thread to be aborted. See System.Security.Permissions.SecurityPermissionFlag.ControlThread.

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0