Dispatcher.VerifyAccess メソッド

定義

呼び出し元のスレッドがこの Dispatcher にアクセスできるかどうかを確認します。Determines whether the calling thread has access to this Dispatcher.

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

例外

呼び出し元のスレッドは、この Dispatcher にアクセスできません。The calling thread does not have access to this Dispatcher.

次の例でVerifyAccessは、を使用して、 Buttonが作成されたスレッドにスレッドがアクセスできるかどうかを判断します。The following example uses VerifyAccess to determine whether a thread has access to the thread that a Button was created on. メソッドは、オブジェクトを引数として受け取ります。これはButton、にキャストされます。The method takes an object as an argument, which is cast to a Button. DispatcherVerifyAccess のメソッドを呼び出して、Buttonスレッドへのアクセスを確認します。The VerifyAccess method on the Dispatcher of the Button is called to verify access to the thread.

呼び出し元のスレッドがにアクセスできるDispatcher場合Button 、はのメンバーにButtonアクセスするだけで更新されます。If the calling thread has access to the Dispatcher, the Button is updated by just accessing the members of the Button.

呼び出し元のスレッドがアクセス権をInvalidOperationException持っていない場合は、がスローされます。If the calling thread does not have access, an InvalidOperationException is thrown. この例では、例外をキャッチし、をButton引数Dispatcher Buttonとして受け取るデリゲートをのにプッシュします。This example catches the exception and pushes a delegate, which accepts a Button as an argument, onto the Dispatcher of the Button. これDispatcherにより、 Buttonの更新作業が実行されます。This Dispatcher will do the work of updating the 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にアクセスできます。Only the thread the Dispatcher is created on may access the Dispatcher.

このメソッドはパブリックです。したがって、すべてのDispatcherスレッドがにアクセスできるかどうかを確認できます。This method is public; therefore, any thread can check to see whether it has access to the Dispatcher.

CheckAccessとのVerifyAccess違いはCheckAccess 、呼び出し元VerifyAccessのスレッドがにアクセスできず、例外をスローする場合にブール値を返します。DispatcherThe difference between CheckAccess and VerifyAccess is CheckAccess returns a Boolean if the calling thread does not have access to the Dispatcher and VerifyAccess throws an exception.

適用対象

こちらもご覧ください