Thread.EndCriticalRegion 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
通知主機執行即將進入一個程式碼區域,該區域中執行緒中止或未處理異常的影響僅限於當前任務。
public:
static void EndCriticalRegion();
public static void EndCriticalRegion();
static member EndCriticalRegion : unit -> unit
Public Shared Sub EndCriticalRegion ()
範例
以下範例示範如何使用 BeginCriticalRegion and EndCriticalRegion 方法將程式碼區塊劃分為關鍵區塊與非關鍵區塊。
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
備註
通用語言執行環境(CLR)的主機,如 Microsoft SQL Server 2005,可以在程式碼的關鍵與非關鍵區域建立不同的故障策略。 關鍵區域是指執行緒中止或未處理異常的影響不限於當前任務。 相較之下,非關鍵程式碼區域的中止或失敗只會影響錯誤發生的任務。
舉例來說,考慮一個任務在持有鎖的同時嘗試分配記憶體。 若記憶體配置失敗,僅中止目前任務不足以確保 的穩定性 AppDomain,因為領域中可能有其他任務等待同一鎖。 如果目前的任務被終止,其他任務可能會陷入死結。
當關鍵區域發生故障時,主機可能會選擇全部卸載, AppDomain 而不是冒險在可能不穩定的狀態下繼續執行。 若要通知主機您的程式碼進入關鍵區域,請撥打 BeginCriticalRegion。 當執行回到非關鍵的程式碼區域時呼叫 EndCriticalRegion 。
在執行於 SQL Server 2005 下的程式碼中使用此方法,則需在最高主機保護層級執行程式碼。