DispatcherObject.CheckAccess Metoda

Definicja

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

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

Zwraca

true jeśli wątek wywołujący ma dostęp do tego obiektu; w przeciwnym razie , false.

Przykłady

W poniższym przykładzie użyto CheckAccess funkcji do określenia, czy wątek ma dostęp do wątku, Button na który został utworzony element. Metoda CheckAccess w obiekcie jest wywoływana Button w celu zweryfikowania dostępu do wątku. Jeśli wątek wywołujący ma dostęp, Button element jest aktualizowany po prostu przez uzyskanie dostępu do członków Buttonobiektu ; w przeciwnym razie delegat, który akceptuje Button argument jako argument, jest publikowany na Dispatcher stronie Button.

// Uses the DispatcherObject.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.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
            // Pushing update method on the Dispatcher of the UI thread
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new UpdateUIDelegate(UpdateButtonUI), theButton);
        }
    }
}
' Uses the DispatcherObject.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.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
            ' Pushing 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

Uwagi

Tylko wątek Dispatcher utworzony przez program może uzyskać dostęp do elementu DispatcherObject.

Każdy wątek może sprawdzić, czy ma on dostęp do tego DispatcherObjectelementu .

Różnica między elementami CheckAccess i VerifyAccess polega na tym, że CheckAccess zwraca wartość logiczną określającą, czy wątek wywołujący ma dostęp do tego DispatcherObject i VerifyAccess zgłasza wyjątek, jeśli wątek wywołujący nie ma dostępu do tego DispatcherObjectelementu .

Wywoływanie tej metody jest identyczne z wywoływaniem CheckAccess skojarzonego Dispatcher obiektu.

Dotyczy