Thread.EndCriticalRegion Método

Definição

Notifica um host de que a execução está prestes a entrar em uma região de código na qual os efeitos de uma exceção sem tratamento ou anulação de thread estã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 BeginCriticalRegion EndCriticalRegion métodos e para dividir um bloco de código em regiões críticas e não críticas.

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.
    }
}
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 o 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 na qual os efeitos de uma anulação de thread ou de uma exceção sem cuidado podem não ser 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 estabilidade do , porque pode haver outras tarefas no domínio aguardando AppDomain o mesmo bloqueio. Se a tarefa atual for encerrada, outras tarefas poderão ser executadas em deadlock.

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

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

Aplica-se a

Confira também