Dispatcher.VerifyAccess メソッド

定義

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

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

例外

呼び出し元のスレッドには、この Dispatcherへのアクセス権がありません。

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

呼び出し元のスレッドが Dispatcherにアクセスできる場合、 ButtonButtonのメンバーにアクセスするだけで更新されます。

呼び出し元のスレッドにアクセス権がない場合は、 InvalidOperationException がスローされます。 この例では、例外をキャッチし、Buttonを引数として受け取るデリゲートをDispatcherButtonにプッシュします。 この 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(),
                "Exception 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にアクセスできるかどうかを確認できます。

CheckAccessVerifyAccessの違いは、呼び出し元のスレッドがCheckAccessにアクセスせず、Dispatcherが例外をスローした場合にブール値を返VerifyAccess

適用対象

こちらもご覧ください