DispatcherObject.CheckAccess メソッド

定義

呼び出し元のスレッドがこの DispatcherObjectにアクセスできるかどうかを判断します。

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

返品

true 呼び出し元のスレッドがこのオブジェクトにアクセスできる場合。それ以外の場合は false

次の例では、 CheckAccess を使用して、 Button が作成されたスレッドにスレッドがアクセスできるかどうかを判断します。 スレッドへのアクセスを確認するために、ButtonCheckAccess メソッドが呼び出されます。 呼び出し元のスレッドにアクセス権がある場合は、ButtonのメンバーにアクセスするだけでButtonが更新されます。それ以外の場合は、Buttonを引数として受け入れるデリゲートがButtonDispatcherにポストされます。

// 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にアクセスできるかどうかを確認できます。

CheckAccessVerifyAccessの違いは、呼び出し元のスレッドがこのDispatcherObjectにアクセスできるかどうかを指定CheckAccessブール値を返し、呼び出し元のスレッドがこのDispatcherObjectにアクセスできない場合に例外をスローVerifyAccessということです。

このメソッドの呼び出しは、関連付けられているDispatcher オブジェクトでCheckAccessを呼び出すことと同じです。

適用対象