Dispatcher.VerifyAccess Methode

Definition

Bestimmt, ob der aufrufende Thread Zugriff auf diesen DispatcherThread hat.

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

Ausnahmen

Der aufrufende Thread hat keinen Zugriff auf diesen DispatcherThread.

Beispiele

Im folgenden Beispiel wird VerifyAccess ermittelt, ob ein Thread Zugriff auf den Thread hat, für den ein Button Thread erstellt wurde. Die Methode verwendet ein Objekt als Argument, das in ein Button. Die VerifyAccess Methode für den Dispatcher Aufruf wird Button aufgerufen, um den Zugriff auf den Thread zu überprüfen.

Wenn der aufrufende Thread Zugriff auf den DispatcherThread hat, wird die Button Aktualisierung durch den Zugriff auf die Member der Button.

Wenn der aufrufende Thread keinen Zugriff hat, wird ein InvalidOperationException Fehler ausgelöst. In diesem Beispiel wird die Ausnahme abgefangen und ein Delegat, der als Button Argument akzeptiert wird, auf die DispatcherButtonStellvertretung verschoben. Dadurch Dispatcher wird die Aktualisierung der ButtonDatei ausgeführt.

// 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

Hinweise

Nur der Thread, auf dem der Dispatcher Thread erstellt wird, kann auf die Dispatcher.

Diese Methode ist öffentlich; Daher kann jeder Thread überprüfen, ob er Zugriff auf die Dispatcher.

Der Unterschied zwischen CheckAccess und VerifyAccess gibt CheckAccess einen Booleschen Wert zurück, wenn der aufrufende Thread keinen Zugriff auf die Dispatcher und VerifyAccess löst eine Ausnahme aus.

Gilt für:

Weitere Informationen