Dispatcher.VerifyAccess Método

Definición

Determina si el subproceso de la llamada tiene acceso a Dispatcher.

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

Excepciones

El subproceso de la llamada no tiene acceso a Dispatcher.

Ejemplos

En el ejemplo siguiente se usa VerifyAccess para determinar si un subproceso tiene acceso al subproceso en el que se creó un Button . El método toma un objeto como argumento, que se convierte en .Button Se VerifyAccess llama al método del Dispatcher objeto Button para comprobar el acceso al subproceso.

Si el subproceso que realiza la llamada tiene acceso a Dispatcher, Button se actualiza simplemente accediendo a los miembros de .Button

Si el subproceso que realiza la llamada no tiene acceso, se produce una InvalidOperationException excepción . En este ejemplo se detecta la excepción y se inserta un delegado, que acepta un Button como argumento, en el Dispatcher Buttonde . Esto Dispatcher hará el trabajo de actualizar .Button

// 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(), 
                "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(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

Comentarios

Solo el subproceso en el que Dispatcher se crea puede acceder a Dispatcher.

Este método es público; por lo tanto, cualquier subproceso puede comprobar si tiene acceso a Dispatcher.

La diferencia entre CheckAccess y VerifyAccess devuelve CheckAccess un valor booleano si el subproceso que realiza la llamada no tiene acceso a Dispatcher y VerifyAccess produce una excepción.

Se aplica a

Consulte también