Thread.EndCriticalRegion 方法

定義

通知主機在即將執行的程式碼區域中,執行緒中止或未處理例外狀況影響的對象只限於目前的工作。

public:
 static void EndCriticalRegion();
public static void EndCriticalRegion ();
static member EndCriticalRegion : unit -> unit
Public Shared Sub EndCriticalRegion ()

範例

下列範例示範如何使用 BeginCriticalRegionEndCriticalRegion 方法來將程式碼區塊分割成重大和非關鍵區域。

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

備註

Common Language Runtime (CLR) 的主機,例如 Microsoft SQL Server 2005,可以為程式碼的重要和非關鍵區域中的失敗建立不同的原則。 重要區域是執行緒中止或未處理的例外狀況效果不限於目前的工作。 相反地,非重大程式碼區域中的中止或失敗只會影響發生錯誤的工作。

例如,請考慮嘗試在保留鎖定時配置記憶體的工作。 如果記憶體配置失敗,中止目前的工作並不足以確保 的 AppDomain 穩定性,因為網域中可能有其他工作正在等候相同的鎖定。 如果目前的工作已終止,其他工作可能會死結。

當重大區域中發生失敗時,主機可能會決定卸載整個 AppDomain ,而不是承擔繼續執行可能不穩定狀態的風險。 若要通知主機您的程式碼正在進入重要區域,請呼叫 BeginCriticalRegion 。 執行 EndCriticalRegion 回到程式碼的非關鍵區域時呼叫。

在 SQL Server 2005 下執行的程式碼中使用這個方法,需要程式碼在最高主機保護層級執行。

適用於

另請參閱