DispatcherObject.CheckAccess 方法

定義

判斷呼叫執行緒是否能存取此 DispatcherObject

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

傳回

true如果呼叫執行緒能存取此物件;否則,。 false

範例

以下範例用 CheckAccess 來判斷執行緒是否能存取 a Button 所建立的執行緒。 CheckAccess呼叫 的方法Button來驗證對執行緒的存取。 若呼叫執行緒有存取權限, Button 則透過存取 的成員 Button來更新;否則,接受 參數 Button 的代理會被貼在 DispatcherButton的 上。

// 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

備註

只有 該 Dispatcher 執行緒所建立的執行緒才能存取 DispatcherObject

任何討論串都可以檢查是否有權限存取這個。DispatcherObject

與 的CheckAccess差異在於 會VerifyAccess回傳一個布林值,指定呼叫執行緒是否有權限存取此值CheckAccess,若DispatcherObject呼叫執行緒無法存取此VerifyAccess節點,則拋出DispatcherObject例外。

呼叫此方法與 CheckAccess 呼叫相關 Dispatcher 物件相同。

適用於