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

注釈

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

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

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

適用対象

こちらもご覧ください