Thread.EndCriticalRegion 方法
定义
通知主机执行将要进入一个代码区域,在该代码区域内线程中止或未经处理异常的影响限于当前任务。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 ()
示例
下面的示例演示如何使用 BeginCriticalRegion 和方法将 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.
}
}
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
注解
公共语言运行时 (CLR) 的宿主,如 Microsoft SQL Server 2005,可以为代码的关键和非关键区域中的故障建立不同的策略。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. 如果内存分配失败,则中止当前任务不足以确保的稳定性 AppDomain ,因为域中的其他任务可能会等待相同的锁。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.
当关键区域发生故障时,主机可能决定卸载整个, AppDomain 而不是在可能不稳定的状态下继续执行的风险。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. 若要通知宿主代码正在进入关键区域,请调用 BeginCriticalRegion 。To inform the host that your code is entering a critical region, call BeginCriticalRegion. EndCriticalRegion当执行返回到非关键代码区域时调用。Call EndCriticalRegion when execution returns to a non-critical region of code.
如果在 SQL Server 2005 下运行的代码中使用此方法,则需要在最高的主机保护级别上运行代码。Using this method in code that runs under SQL Server 2005 requires the code to be run at the highest host protection level.