Thread.EndCriticalRegion Método

Definição

Notifica um host de que a execução está prestes a inserir uma região de código na qual os efeitos de uma exceção sem tratamento ou anulação de thread são limitados à tarefa atual.

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

Exemplos

O exemplo a seguir demonstra o uso dos métodos e EndCriticalRegion do BeginCriticalRegion uso 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

Comentários

Hosts do CLR (Common Language Runtime), como Microsoft SQL Server 2005, podem estabelecer políticas diferentes para falhas em regiões críticas e não críticas de código. Uma região crítica é aquela em que os efeitos de uma anulação de thread ou uma exceção sem tratamento podem não estar limitados à tarefa atual. Por outro lado, uma anulação ou falha em uma região não crítica do código afeta apenas a tarefa na qual 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, anular a tarefa atual não será suficiente para garantir a AppDomainestabilidade do, pois pode haver outras tarefas no domínio aguardando o mesmo bloqueio. Se a tarefa atual for encerrada, outras tarefas poderão ser bloqueadas.

Quando ocorre uma falha em uma região crítica, o host pode decidir descarregar o todo AppDomain em vez de correr o risco de continuar a execução em um estado potencialmente instável. Para informar ao host que seu código está inserindo uma região crítica, chame BeginCriticalRegion. Chame EndCriticalRegion quando a execução retornar a uma região não crítica do código.

Usar esse método no código executado em SQL Server 2005 requer que o código seja executado no nível mais alto de proteção do host.

Aplica-se a

Confira também