Dispatcher.CheckAccess Metoda

Definicja

Określa, czy wątek wywołujący jest wątkiem skojarzonym z tym Dispatcherelementem .

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 jest wątkiem skojarzonym z tym Dispatcher; w przeciwnym razie false.

Przykłady

W poniższym przykładzie użyto CheckAccess metody do określenia, czy wątek ma dostęp do elementu Button. Metoda CheckAccess skojarzona Dispatcher z elementem 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 uzyskanie dostępu do elementów członkowskich obiektu Button; w przeciwnym razie delegat, który akceptuje Button jako argument, jest umieszczany w obiekcie Dispatcher. Funkcja Dispatcher deleguje pracę aktualizowania elementu 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

Uwagi

Tylko utworzony DispatcherDispatcherObject obiekt może uzyskiwać dostęp do obiektu . Użyj polecenia Invoke lub BeginInvoke , aby uzyskać dostęp do obiektu z innego wątku.

CheckAccess można wywołać z dowolnego wątku.

Różnica między elementami CheckAccess i VerifyAccess zwraca CheckAccess wartość logiczną wskazującą, czy wątek wywołujący ma dostęp do obiektu Dispatcher i VerifyAccess zgłasza wyjątek.

Dotyczy

Zobacz też