Thread.EndCriticalRegion Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception are limited to the current task.
public:
static void EndCriticalRegion();
public static void EndCriticalRegion ();
static member EndCriticalRegion : unit -> unit
Public Shared Sub EndCriticalRegion ()
The following example demonstrates the use of the BeginCriticalRegion and EndCriticalRegion methods to divide a block of code into critical and non-critical regions.
using namespace System::Threading;
public ref class MyUtility
{
public:
void PerformTask()
{
// Code in this region can be aborted without affecting
// other tasks.
//
Thread::BeginCriticalRegion();
//
// The host might decide to unload the application domain
// if a failure occurs in this code region.
//
Thread::EndCriticalRegion();
//
// Code in this region can be aborted without affecting
// other tasks.
}
};
using System.Threading;
public class MyUtility
{
public void PerformTask()
{
// Code in this region can be aborted without affecting
// other tasks.
//
Thread.BeginCriticalRegion();
//
// The host might decide to unload the application domain
// if a failure occurs in this code region.
//
Thread.EndCriticalRegion();
//
// Code in this region can be aborted without affecting
// other tasks.
}
}
open System.Threading
let performTask () =
// Code in this region can be aborted without affecting
// other tasks.
//
Thread.BeginCriticalRegion()
//
// The host might decide to unload the application domain
// if a failure occurs in this code region.
//
Thread.EndCriticalRegion()
//
// Code in this region can be aborted without affecting
// other tasks.
Imports System.Threading
Public Class MyUtility
Public Sub PerformTask()
' Code in this region can be aborted without affecting
' other tasks.
'
Thread.BeginCriticalRegion()
'
' The host might decide to unload the application domain
' if a failure occurs in this code region.
'
Thread.EndCriticalRegion()
' Code in this region can be aborted without affecting
' other tasks.
End Sub
End Class
Hosts of the common language runtime (CLR), such as Microsoft SQL Server 2005, can establish different policies for failures in critical and non-critical regions of code. A critical region is one in which the effects of a thread abort or an unhandled exception might not be limited to the current task. By contrast, an abort or failure in a non-critical region of code affects only the task in which the error occurs.
For example, consider a task that attempts to allocate memory while holding a lock. If the memory allocation fails, aborting the current task is not sufficient to ensure stability of the AppDomain, because there can be other tasks in the domain waiting for the same lock. If the current task is terminated, other tasks could be deadlocked.
When a failure occurs in a critical region, the host might decide to unload the entire AppDomain rather than take the risk of continuing execution in a potentially unstable state. To inform the host that your code is entering a critical region, call BeginCriticalRegion. Call EndCriticalRegion when execution returns to a non-critical region of code.
Using this method in code that runs under SQL Server 2005 requires the code to be run at the highest host protection level.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: