Export (0) Print
Expand All

Dispatcher.VerifyAccess Method

Determines whether the calling thread has access to this Dispatcher.

Namespace:  System.Windows.Threading
Assembly:  WindowsBase (in WindowsBase.dll)

public void VerifyAccess()

ExceptionCondition
InvalidOperationException

The calling thread does not have access to this Dispatcher.

Only the thread the Dispatcher is created on may access the Dispatcher.

This method is public; therefore, any thread can check to see whether it has access to the Dispatcher.

The 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.

The following example uses VerifyAccess to determine whether a thread has access to the thread that a Button was created on. The method takes an object as an argument, which is cast to a Button. The VerifyAccess method on the Dispatcher of the Button is called to verify access to the thread.

If the calling thread has access to the Dispatcher, the Button is updated by just accessing the members of the Button.

If the calling thread does not have access, an InvalidOperationException is thrown. This example catches the exception and pushes a delegate, which accepts a Button as an argument, onto the Dispatcher of the 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);
        }
    }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft