Dispatcher.VerifyAccess Metoda

Definice

Určuje, zda má volající vlákno přístup k tomuto Dispatcher.

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

Výjimky

Volající vlákno nemá přístup k tomuto Dispatcher.

Příklady

Následující příklad používá VerifyAccess k určení, zda vlákno má přístup k vláknu, na které Button byl vytvořen. Metoda přebírá objekt jako argument, který se přetypuje na Button. Metoda VerifyAccess na Dispatcher Button volání je volána k ověření přístupu k vláknu.

Pokud má volající vlákno přístup k sadě Dispatcher, Button aktualizuje se pouze přístupem k členům .Button

Pokud volající vlákno nemá přístup, vyvolá se chyba InvalidOperationException . Tento příklad zachytí výjimku a nasdílí delegáta, který přijímá Button jako argument na Dispatcher straně Button. Tím Dispatcher se provede aktualizace Buttonsouboru .

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

Poznámky

Přístup může získat pouze vlákno, Dispatcher na které je vytvořeno Dispatcher.

Tato metoda je veřejná; jakékoli vlákno proto může zkontrolovat, zda má přístup k sadě Dispatcher.

Rozdíl mezi CheckAccess a VerifyAccess je CheckAccess vrácen logická hodnota, pokud volající vlákno nemá přístup k sadě Dispatcher a VerifyAccess vyvolá výjimku.

Platí pro

Viz také