Skip to content

Latest commit

 

History

History
77 lines (67 loc) · 4.35 KB

File metadata and controls

77 lines (67 loc) · 4.35 KB
title Destroying Threads
ms.custom
ms.date 03/30/2017
ms.prod .net
ms.reviewer
ms.suite
ms.technology dotnet-standard
ms.tgt_pltfrm
ms.topic article
dev_langs
csharp
vb
helpviewer_keywords
destroying threads
threading [.NET Framework], destroying threads
ms.assetid df54e648-c5d1-47c9-bd29-8e4438c1db6d
caps.latest.revision 12
author rpetrusha
ms.author ronpet
manager wpickett
ms.workload
dotnet
dotnetcore

Destroying Threads

The xref:System.Threading.Thread.Abort%2A method is used to stop a managed thread permanently. When you call xref:System.Threading.Thread.Abort%2A, the common language runtime throws a xref:System.Threading.ThreadAbortException in the target thread, which the target thread can catch. For more information, see xref:System.Threading.Thread.Abort%2A?displayProperty=nameWithType.

Note

If a thread is executing unmanaged code when its xref:System.Threading.Thread.Abort%2A method is called, the runtime marks it xref:System.Threading.ThreadState.AbortRequested?displayProperty=nameWithType. The exception is thrown when the thread returns to managed code.

Once a thread is aborted, it cannot be restarted.

The xref:System.Threading.Thread.Abort%2A method does not cause the thread to abort immediately, because the target thread can catch the xref:System.Threading.ThreadAbortException and execute arbitrary amounts of code in a finally block. You can call xref:System.Threading.Thread.Join%2A?displayProperty=nameWithType if you need to wait until the thread has ended. xref:System.Threading.Thread.Join%2A?displayProperty=nameWithType is a blocking call that does not return until the thread has actually stopped executing or an optional timeout interval has elapsed. The aborted thread could call the xref:System.Threading.Thread.ResetAbort%2A method or perform unbounded processing in a finally block, so if you do not specify a timeout, the wait is not guaranteed to end.

Threads that are waiting on a call to the xref:System.Threading.Thread.Join%2A?displayProperty=nameWithType method can be interrupted by other threads that call xref:System.Threading.Thread.Interrupt%2A?displayProperty=nameWithType.

Handling ThreadAbortException

If you expect your thread to be aborted, either as a result of calling xref:System.Threading.Thread.Abort%2A from your own code or as a result of unloading an application domain in which the thread is running (xref:System.AppDomain.Unload%2A?displayProperty=nameWithType uses xref:System.Threading.Thread.Abort%2A?displayProperty=nameWithType to terminate threads), your thread must handle the xref:System.Threading.ThreadAbortException and perform any final processing in a finally clause, as shown in the following code.

Try  
    ' Code that is executing when the thread is aborted.  
Catch ex As ThreadAbortException  
    ' Clean-up code can go here.  
    ' If there is no Finally clause, ThreadAbortException is  
    ' re-thrown by the system at the end of the Catch clause.   
Finally  
    ' Clean-up code can go here.  
End Try  
' Do not put clean-up code here, because the exception   
' is rethrown at the end of the Finally clause.  
try   
{  
    // Code that is executing when the thread is aborted.  
}   
catch (ThreadAbortException ex)   
{  
    // Clean-up code can go here.  
    // If there is no Finally clause, ThreadAbortException is  
    // re-thrown by the system at the end of the Catch clause.   
}  
// Do not put clean-up code here, because the exception   
// is rethrown at the end of the Finally clause.  

Your clean-up code must be in the catch clause or the finally clause, because a xref:System.Threading.ThreadAbortException is rethrown by the system at the end of the finally clause, or at the end of the catch clause if there is no finally clause.

You can prevent the system from rethrowing the exception by calling the xref:System.Threading.Thread.ResetAbort%2A?displayProperty=nameWithType method. However, you should do this only if your own code caused the xref:System.Threading.ThreadAbortException.

See Also

xref:System.Threading.ThreadAbortException
xref:System.Threading.Thread
Using Threads and Threading