Dispatcher.CheckAccess メソッド

定義

呼び出し元のスレッドがこの Dispatcherに関連付けられているスレッドであるかどうかを判断します。

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

返品

true 呼び出し元のスレッドがこの Dispatcherに関連付けられているスレッドの場合は。それ以外の場合は false

次の例では、 CheckAccess を使用して、スレッドが Buttonにアクセスできるかどうかを判断します。 スレッドへのアクセスを確認するために、CheckAccessに関連付けられているDispatcherButton メソッドが呼び出されます。 呼び出し元のスレッドがDispatcherにアクセスできる場合は、Buttonのメンバーにアクセスすることで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

注釈

Dispatcherが作成されたDispatcherObjectのみがオブジェクトにアクセスできます。 InvokeまたはBeginInvokeを使用して、別のスレッドからオブジェクトにアクセスします。

CheckAccess は、任意のスレッドから呼び出すことができます。

CheckAccessVerifyAccessの違いはCheckAccess呼び出し元のスレッドがDispatcherにアクセスできるかどうかを示すブール値を返し、VerifyAccessは例外をスローします。

適用対象

こちらもご覧ください