Dispatcher.CheckAccess Metoda

Definice

Určuje, zda volající vlákno je vlákno přidružené k tomuto Dispatcher.

public:
 bool CheckAccess();
public bool CheckAccess ();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean

Návraty

true pokud volající vlákno je vlákno přidružené k tomuto Dispatcher; v opačném případě , false.

Příklady

Následující příklad používá CheckAccess k určení, zda má vlákno přístup k objektu Button. Metoda CheckAccess na Dispatcher přidružené k Button je volána k ověření přístupu k vláknu. Pokud má volající vlákno přístup k objektu Dispatcher, Button aktualizuje se přístupem ke členům Button. V opačném případě je delegát, který přijímá Button jako argument, umístěn do objektu Dispatcher. Bude Dispatcher delegovat práci při aktualizaci .Button

// Uses the Dispatcher.CheckAccess method to determine if 
// the calling thread has access to the thread the UI object is on.
private void TryToUpdateButtonCheckAccess(object uiObject)
{
    Button theButton = uiObject as Button;

    if (theButton != null)
    {
        // Checking if this thread has access to the object.
        if (theButton.Dispatcher.CheckAccess())
        {
            // This thread has access so it can update the UI thread.
            UpdateButtonUI(theButton);
        }
        else
        {
            // This thread does not have access to the UI thread.
            // Place the update method on the Dispatcher of the UI thread.
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new UpdateUIDelegate(UpdateButtonUI), theButton);
        }
    }
}
' Uses the Dispatcher.CheckAccess method to determine if 
' the calling thread has access to the thread the UI object is on.
Private Sub TryToUpdateButtonCheckAccess(ByVal uiObject As Object)
    Dim theButton As Button = TryCast(uiObject, Button)

    If theButton IsNot Nothing Then
        ' Checking if this thread has access to the object.
        If theButton.Dispatcher.CheckAccess() Then
            ' This thread has access so it can update the UI thread.
            UpdateButtonUI(theButton)
        Else
            ' This thread does not have access to the UI thread.
            ' Place the update method on the Dispatcher of the UI thread.
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New UpdateUIDelegate(AddressOf UpdateButtonUI), theButton)
        End If
    End If
End Sub

Poznámky

K objektu Dispatcher může přistupovat pouze objekt, na DispatcherObject který je vytvořen. Pro přístup k objektu z jiného vlákna použijte Invoke nebo BeginInvoke .

CheckAccess lze volat z libovolného vlákna.

Rozdíl mezi CheckAccess a VerifyAccess vrátí CheckAccess logickou hodnotu označující, zda volající vlákno má přístup k Dispatcher a VerifyAccess vyvolá výjimku.

Platí pro

Viz také