Dispatcher.CheckAccess Método

Definição

Determina se o thread que chama é o thread associado a este Dispatcher.

public:
 bool CheckAccess();
public bool CheckAccess();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean

Devoluções

true se a thread que chama for a thread associada a este Dispatcher; caso contrário, false.

Exemplos

O exemplo seguinte serve CheckAccess para determinar se um thread tem acesso a um Button. O CheckAccess método associado Dispatcher ao Button é chamado para verificar o acesso ao thread. Se o thread chamador tiver acesso ao Dispatcher, o Button é atualizado acedendo aos membros do Button; caso contrário, um delegado, que aceita a Button como argumento, é colocado sobre o Dispatcher. O Dispatcher irá delegar o trabalho de atualizar o Button.

// Uses the Dispatcher.CheckAccess method to determine if
// the calling thread has access to the thread the UI object is on.
private void TryToUpdateButtonCheckAccess(object uiObject)
{
    Button theButton = uiObject as Button;

    if (theButton != null)
    {
        // Checking if this thread has access to the object.
        if (theButton.Dispatcher.CheckAccess())
        {
            // This thread has access so it can update the UI thread.
            UpdateButtonUI(theButton);
        }
        else
        {
            // This thread does not have access to the UI thread.
            // Place the update method on the Dispatcher of the UI thread.
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new UpdateUIDelegate(UpdateButtonUI), theButton);
        }
    }
}
' Uses the Dispatcher.CheckAccess method to determine if 
' the calling thread has access to the thread the UI object is on.
Private Sub TryToUpdateButtonCheckAccess(ByVal uiObject As Object)
    Dim theButton As Button = TryCast(uiObject, Button)

    If theButton IsNot Nothing Then
        ' Checking if this thread has access to the object.
        If theButton.Dispatcher.CheckAccess() Then
            ' This thread has access so it can update the UI thread.
            UpdateButtonUI(theButton)
        Else
            ' This thread does not have access to the UI thread.
            ' Place the update method on the Dispatcher of the UI thread.
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New UpdateUIDelegate(AddressOf UpdateButtonUI), theButton)
        End If
    End If
End Sub

Observações

Apenas o Dispatcher que a DispatcherObject é criado em pode aceder ao objeto. Usar Invoke ou BeginInvoke aceder ao objeto a partir de outro tópico.

CheckAccess pode ser chamado a partir de qualquer thread.

A diferença entre CheckAccess e VerifyAccess é CheckAccess devolve um Booleano que indica se o thread que chama tem acesso ao Dispatcher e VerifyAccess lança uma exceção.

Aplica-se a

Ver também