Thread.BeginCriticalRegion メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
スレッドの中止またはハンドルされない例外の影響がアプリケーション ドメイン内の他のタスクを危険にさらす可能性があるコード領域を入力しようとしていることをホストに通知します。
public:
static void BeginCriticalRegion();
public static void BeginCriticalRegion();
static member BeginCriticalRegion : unit -> unit
Public Shared Sub BeginCriticalRegion ()
例
次の例では、 BeginCriticalRegion メソッドと 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
注釈
Microsoft SQL Server 2005 など、共通言語ランタイム (CLR) のホストは、コードのクリティカルリージョンと非クリティカルリージョンでの障害に対して異なるポリシーを確立できます。 重大な領域は、スレッドの中止またはハンドルされない例外の影響が現在のタスクに限定されない可能性がある領域です。 これに対し、コードの重要でない領域での中止または失敗は、エラーが発生したタスクにのみ影響します。
たとえば、ロックを保持しながらメモリを割り当てようとするタスクを考えてみます。 メモリ割り当てが失敗した場合、ドメイン内に同じロックを待機している他のタスクが存在する可能性があるため、 AppDomainの安定性を確保するには、現在のタスクを中止するだけでは不十分です。 現在のタスクが終了すると、他のタスクがデッドロックする可能性があります。
重大なリージョンで障害が発生した場合、ホストは、不安定な状態で実行を継続するリスクを取るのではなく、 AppDomain 全体をアンロードすることを決定する場合があります。 コードが重要なリージョンに入っていることをホストに通知するには、 BeginCriticalRegionを呼び出します。 実行が重要でないコード領域に戻ったときに EndCriticalRegion を呼び出します。
SQL Server 2005 で実行されるコードでこのメソッドを使用するには、最も高いホスト保護レベルでコードを実行する必要があります。