共用方式為


DispatcherObject.CheckAccess 方法

定義

判斷呼叫的執行是否可以存取這個 DispatcherObject

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

傳回

Boolean

如果呼叫的執行緒可以存取這個物件則為 true,否則為 false

範例

下列範例會使用 CheckAccess 來判斷線程是否能夠存取建立所在的執行緒 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

VerifyAccess 之間的差異 CheckAccess 在於會傳回 Boolean, CheckAccess 指定呼叫執行緒是否具有這個 DispatcherObject 的存取權,如果呼叫執行緒沒有這個 DispatcherObject 的存取權,則會 VerifyAccess 擲回例外狀況。

呼叫這個方法與在相關聯的 Dispatcher 物件上呼叫 CheckAccess 相同。

適用於