Dispatcher.CheckAccess 方法

定義

判斷呼叫的執行緒是否為與這個 Dispatcher 關聯的執行緒。

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

傳回

如果呼叫的執行緒為與這個 Dispatcher 關聯的執行緒則為 true,否則為 false

範例

下列範例會使用 CheckAccess 來判斷線程是否具有 的 Button 存取權。 會 CheckAccess 呼叫 與 Button 相關聯 上的 Dispatcher 方法,以驗證執行緒的存取權。 如果呼叫執行緒可以存取 DispatcherButton 則會藉由存取 的成員來更新 ;否則,接受 Button 做為引數的 Button 委派會放在 。 Dispatcher Dispatcher將會委派更新 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

備註

只有 在 上建立 的 DispatcherDispatcherObject 可以存取 物件。 使用 InvokeBeginInvoke 從不同的執行緒存取 物件。

CheckAccess 可以從任何執行緒呼叫。

和 之間的差異 CheckAccessCheckAccess 傳回 Boolean,指出呼叫執行緒是否具有 的 Dispatcher 存取權,並 VerifyAccessVerifyAccess 回例外狀況。

適用於

另請參閱