Dispatcher.CheckAccess Método

Definição

Determina se o thread de chamada é o thread associado a esse Dispatcher.

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

Retornos

Boolean

true se o thread de chamada é o thread associado a esse Dispatcher; caso contrário, false.

Exemplos

O exemplo a seguir usa CheckAccess para determinar se um thread tem acesso a um Button. O CheckAccess método no Dispatcher associado é Button chamado para verificar o acesso ao thread. Se o thread de chamada tiver acesso ao Dispatcher, ele Button será atualizado acessando os membros do Button; caso contrário, um delegado, que aceita um Button como argumento, será colocado no Dispatcher. O Dispatcher will delegará o trabalho de atualização do 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

Comentários

Somente o Dispatcher que DispatcherObject um é criado pode acessar o objeto. Use Invoke ou BeginInvoke acesse o objeto de um thread diferente.

CheckAccess pode ser chamado de qualquer thread.

A diferença entre CheckAccess e VerifyAccess é CheckAccess retorna um booliano indicando se o thread de chamada tem acesso e VerifyAccess Dispatcher gera uma exceção.

Aplica-se a

Confira também