Dispatcher.VerifyAccess Metoda

Definicja

Określa, czy wątek wywołujący ma dostęp do tego Dispatcherelementu .

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

Wyjątki

Wątek wywołujący nie ma dostępu do tego Dispatcherelementu .

Przykłady

W poniższym przykładzie użyto VerifyAccess metody do określenia, czy wątek ma dostęp do wątku, w ramach którego Button utworzono element . Metoda przyjmuje obiekt jako argument, który jest rzutowy na Buttonobiekt . Metoda VerifyAccess w obiekcie Dispatcher Button jest wywoływana w celu zweryfikowania dostępu do wątku.

Jeśli wątek wywołujący ma dostęp do Dispatcherelementu , Button element jest aktualizowany przez dostęp tylko do elementów członkowskich obiektu Button.

Jeśli wątek wywołujący nie ma dostępu, InvalidOperationException zostanie zgłoszony element . W tym przykładzie przechwytuje wyjątek i wypycha Button delegata, który akceptuje element jako argument na Dispatcher Buttonobiekcie . Spowoduje to Dispatcher wykonanie pracy podczas aktualizowania elementu 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

Uwagi

Tylko wątek, na który Dispatcher jest tworzony, może uzyskać dostęp do elementu Dispatcher.

Ta metoda jest publiczna; w związku z tym każdy wątek może sprawdzić, czy ma dostęp do elementu Dispatcher.

Różnica między elementami CheckAccess i VerifyAccess zwraca CheckAccess wartość logiczną, jeśli wątek wywołujący nie ma dostępu do elementu Dispatcher i VerifyAccess zgłasza wyjątek.

Dotyczy

Zobacz też