Dispatcher.VerifyAccess Método

Definição

Determina se o thread que chama tem acesso a este Dispatcher.

public:
 void VerifyAccess();
public void VerifyAccess();
member this.VerifyAccess : unit -> unit
Public Sub VerifyAccess ()

Exceções

O fio que chama não tem acesso a este Dispatcher.

Exemplos

O exemplo seguinte serve VerifyAccess para determinar se um thread tem acesso ao thread onde a Button foi criado. O método toma um objeto como argumento, que é lançado para um Button. O VerifyAccess método no Dispatcher de Button é chamado para verificar o acesso ao thread.

Se o thread que chama tem acesso ao Dispatcher, o Button é atualizado apenas acedendo aos membros do Button.

Se o thread que chama não tiver acesso, um InvalidOperationException é lançado. Este exemplo detém a exceção e empurra um delegado, que aceita a Button como argumento, para o Dispatcher de .Button Isto Dispatcher fará o trabalho de atualizar o Buttonarquivo .

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

    if (theButton != null)
    {
        try
        {
            // Check if this thread has access to this object.
            theButton.Dispatcher.VerifyAccess();

            // The thread has access to the object, so update the UI.
            UpdateButtonUI(theButton);
        }

        // Cannot access objects on the thread.
        catch (InvalidOperationException e)
        {
            // Exception Error Message.
            MessageBox.Show("Exception ToString: \n\n" + e.ToString(),
                "Exception Caught! Thrown During AccessVerify().");

            MessageBox.Show("Pushing job onto UI Thread Dispatcher");

            // Placing job onto the Dispatcher of the UI Thread.
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new UpdateUIDelegate(UpdateButtonUI), theButton);
        }
    }
}
' Uses the Dispatcher.VerifyAccess method to determine if 
' the calling thread has access to the thread the UI object is on.
Private Sub TryToUpdateButtonVerifyAccess(ByVal uiObject As Object)
    Dim theButton As Button = TryCast(uiObject, Button)

    If theButton IsNot Nothing Then
        Try
            ' Check if this thread has access to this object.
            theButton.Dispatcher.VerifyAccess()

            ' The thread has access to the object, so update the UI.
            UpdateButtonUI(theButton)

            ' Cannot access objects on the thread.
        Catch e As InvalidOperationException
            ' Exception Error Message.
            MessageBox.Show("Exception ToString: " & vbLf & vbLf & e.ToString(), "Execption Caught! Thrown During AccessVerify().")

            MessageBox.Show("Pushing job onto UI Thread Dispatcher")

            ' Placing job onto the Dispatcher of the UI Thread.
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New UpdateUIDelegate(AddressOf UpdateButtonUI), theButton)
        End Try
    End If
End Sub

Observações

Apenas o thread em que o Dispatcher é criado pode aceder ao Dispatcher.

Este método é público; Portanto, qualquer thread pode verificar se tem acesso ao Dispatcher.

A diferença entre CheckAccess e VerifyAccess s CheckAccess devolve um Booleano se o thread que chama não tiver acesso ao Dispatcher e VerifyAccess lançar uma exceção.

Aplica-se a

Ver também