Dispatcher.VerifyAccess メソッド

定義

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

public:
 void VerifyAccess();
public void VerifyAccess ();
member this.VerifyAccess : unit -> unit
Public Sub VerifyAccess ()

例外

呼び出し元のスレッドは、この Dispatcher にアクセスできません。

次の例では、スレッドが作成されたスレッドButtonにアクセスできるかどうかを判断するために使用VerifyAccessします。 このメソッドは、オブジェクトを引数として受け取り、オブジェクトを Button. のVerifyAccessメソッドDispatcherButtonは、スレッドへのアクセスを確認するために呼び出されます。

呼び出し元のスレッドがアクセスできるDispatcher場合はButtonButton .

呼び出し元のスレッドにアクセス権がない場合は、an InvalidOperationException がスローされます。 この例では、例外をキャッチし、引数として Dispatcher Buttona Button を受け取るデリゲートを . これは Dispatcher 、更新の作業を Button行います.

// Uses the Dispatcher.VerifyAccess method to determine if 
// the calling thread has access to the thread the UI object is on.
private void TryToUpdateButtonVerifyAccess(object uiObject)
{
    Button theButton = uiObject as Button;

    if (theButton != null)
    {
        try
        {   
            // Check if this thread has access to this object.
            theButton.Dispatcher.VerifyAccess();

            // The thread has access to the object, so update the UI.
            UpdateButtonUI(theButton);
        }

        // Cannot access objects on the thread.
        catch (InvalidOperationException e)
        {
            // Exception Error Message.
            MessageBox.Show("Exception ToString: \n\n" + e.ToString(), 
                "Execption Caught! Thrown During AccessVerify().");

            MessageBox.Show("Pushing job onto UI Thread Dispatcher");

            // Placing job onto the Dispatcher of the UI Thread.
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new UpdateUIDelegate(UpdateButtonUI), theButton);
        }
    }
}
' Uses the Dispatcher.VerifyAccess method to determine if 
' the calling thread has access to the thread the UI object is on.
Private Sub TryToUpdateButtonVerifyAccess(ByVal uiObject As Object)
    Dim theButton As Button = TryCast(uiObject, Button)

    If theButton IsNot Nothing Then
        Try
            ' Check if this thread has access to this object.
            theButton.Dispatcher.VerifyAccess()

            ' The thread has access to the object, so update the UI.
            UpdateButtonUI(theButton)

            ' Cannot access objects on the thread.
        Catch e As InvalidOperationException
            ' Exception Error Message.
            MessageBox.Show("Exception ToString: " & vbLf & vbLf & e.ToString(), "Execption Caught! Thrown During AccessVerify().")

            MessageBox.Show("Pushing job onto UI Thread Dispatcher")

            ' Placing job onto the Dispatcher of the UI Thread.
            theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New UpdateUIDelegate(AddressOf UpdateButtonUI), theButton)
        End Try
    End If
End Sub

注釈

にアクセスDispatcherできるのは、Dispatcher作成されたスレッドだけです。

このメソッドはパブリックです。したがって、任意のスレッドがアクセスできるかどうかを Dispatcher確認できます。

呼び出し元のスレッドがアクセスVerifyAccessDispatcher権を持っていない場合、および呼び出し元のスレッドが例外をスローする場合、and VerifyAccess の差CheckAccessCheckAccessブール値を返します。

適用対象

こちらもご覧ください