Dispatcher.CheckAccess Yöntem

Tanım

Çağıran iş parçacığının bu Dispatcherile ilişkili iş parçacığı olup olmadığını belirler.

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

Döndürülenler

true çağıran iş parçacığı bu Dispatcherile ilişkili iş parçacığı ise; değilse, false.

Örnekler

Aşağıdaki örnek, bir iş parçacığının öğesine erişimi olup olmadığını belirlemek için ButtonkullanırCheckAccess. CheckAccess ile Button ilişkili yöntemiDispatcher, iş parçacığına erişimi doğrulamak için çağrılır. Çağıran iş parçacığının Dispatcheröğesine erişimi varsa, Button öğesinin Buttonüyelerine erişilerek güncelleştirilir; aksi takdirde, bağımsız değişken olarak kabul eden bir Button temsilci üzerine Dispatcheryerleştirilir. , Dispatcher güncelleştirme işini temsilci olarak verir 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

Açıklamalar

DispatcherDispatcherObject Yalnızca üzerinde oluşturulan nesneye erişebilir. Nesneye farklı bir iş parçacığından erişmek için veya BeginInvoke kullanınInvoke.

CheckAccess herhangi bir iş parçacığından çağrılabilir.

ile arasındaki CheckAccessVerifyAccess fark, CheckAccess çağıran iş parçacığının öğesine erişimi DispatcherVerifyAccess olup olmadığını ve bir özel durum oluşturup oluşturmadığını belirten bir Boole döndürür.

Şunlara uygulanır

Ayrıca bkz.