Thread.BeginCriticalRegion Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Notifica um host que a execução está prestes a entrar numa região de código em que os efeitos de um aborto de thread ou exceção não tratada podem pôr em risco outras tarefas no domínio da aplicação.
public:
static void BeginCriticalRegion();
public static void BeginCriticalRegion();
static member BeginCriticalRegion : unit -> unit
Public Shared Sub BeginCriticalRegion ()
Exemplos
O exemplo seguinte demonstra a utilização dos BeginCriticalRegion métodos e EndCriticalRegion para dividir um bloco de código em regiões críticas e não críticas.
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
Observações
Os hosts do runtime de linguagem comum (CLR), como o Microsoft SQL Server 2005, podem estabelecer diferentes políticas para falhas em regiões críticas e não críticas do código. Uma região crítica é aquela em que os efeitos de um aborto de thread ou de uma exceção não tratada podem não se limitar à tarefa atual. Em contraste, um aborto ou falha numa região não crítica do código afeta apenas a tarefa em que o erro ocorre.
Por exemplo, considere uma tarefa que tenta alocar memória enquanto mantém um bloqueio. Se a alocação de memória falhar, abortar a tarefa atual não é suficiente para garantir a estabilidade do AppDomain, pois podem existir outras tarefas no domínio à espera do mesmo bloqueio. Se a tarefa atual for terminada, outras tarefas podem ficar bloqueadas.
Quando ocorre uma falha numa região crítica, o host pode decidir descarregar a totalidade AppDomain em vez de correr o risco de continuar a execução num estado potencialmente instável. Para informar o anfitrião de que o seu código está a entrar numa região crítica, ligue BeginCriticalRegionpara . Chame EndCriticalRegion quando a execução regressa a uma região de código não crítica.
Usar este método em código que corre sob SQL Server 2005 exige que o código seja executado ao nível mais alto de proteção do host.