Dispatcher.VerifyAccess Method

Definition

Determina si el subproceso de la llamada tiene acceso a Dispatcher.Determines whether the calling thread has access to this Dispatcher.

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

Exceptions

El subproceso de la llamada no tiene acceso a Dispatcher.The calling thread does not have access to this Dispatcher.

Examples

En el ejemplo siguiente se usa VerifyAccess para determinar si un subproceso tiene acceso al subproceso en el que se creó un Button.The following example uses VerifyAccess to determine whether a thread has access to the thread that a Button was created on. El método toma un objeto como argumento, que se convierte en un Button.The method takes an object as an argument, which is cast to a Button. Se llama al método VerifyAccess en el Dispatcher de la Button para comprobar el acceso al subproceso.The VerifyAccess method on the Dispatcher of the Button is called to verify access to the thread.

Si el subproceso que realiza la llamada tiene acceso al Dispatcher, el Button se actualiza simplemente teniendo acceso a los miembros de la Button.If the calling thread has access to the Dispatcher, the Button is updated by just accessing the members of the Button.

Si el subproceso que realiza la llamada no tiene acceso, se produce una InvalidOperationException.If the calling thread does not have access, an InvalidOperationException is thrown. En este ejemplo se detecta la excepción y se envía un delegado, que acepta un Button como argumento, en el Dispatcher de la Button.This example catches the exception and pushes a delegate, which accepts a Button as an argument, onto the Dispatcher of the Button. Esta Dispatcher realizará el trabajo de actualización de la Button.This Dispatcher will do the work of updating the 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

Remarks

Solo el subproceso en el que se crea el Dispatcher puede tener acceso al Dispatcher.Only the thread the Dispatcher is created on may access the Dispatcher.

Este método es público; por lo tanto, cualquier subproceso puede comprobar si tiene acceso a la Dispatcher.This method is public; therefore, any thread can check to see whether it has access to the Dispatcher.

La diferencia entre CheckAccess y VerifyAccess es CheckAccess devuelve un valor booleano si el subproceso que realiza la llamada no tiene acceso al Dispatcher y VerifyAccess produce una excepción.The difference between CheckAccess and VerifyAccess is CheckAccess returns a Boolean if the calling thread does not have access to the Dispatcher and VerifyAccess throws an exception.

Applies to

See also