Dispatcher.CheckAccess Metod

Definition

Avgör om den anropande tråden är den tråd som är associerad med den här Dispatcher.

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

Returer

true om den anropande tråden är den tråd som är associerad med detta Dispatcher, annars , false.

Exempel

I följande exempel används CheckAccess för att avgöra om en tråd har åtkomst till en Button. Metoden CheckAccess för den Dispatcher associerade med Button anropas för att verifiera åtkomsten till tråden. Om den anropande tråden DispatcherButton har åtkomst till , uppdateras genom åtkomst till medlemmarna i Button. Annars placeras ett ombud, som accepterar ett Button som argument, på Dispatcher. Kommer Dispatcher att delegera arbetet med att uppdatera 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

Kommentarer

Endast den Dispatcher som en DispatcherObject skapas på kan komma åt objektet. Använd Invoke eller BeginInvoke för att komma åt objektet från en annan tråd.

CheckAccess kan anropas från valfri tråd.

Skillnaden mellan CheckAccess och VerifyAccess returnerar CheckAccess ett booleskt värde som anger om den anropande tråden har åtkomst till Dispatcher och VerifyAccess genererar ett undantag.

Gäller för

Se även