DependencyObject.Dispatcher property

Applies to Windows and Windows Phone

Gets the CoreDispatcher that this object is associated with.

Syntax


public CoreDispatcher Dispatcher { get; }

Property value

Type: CoreDispatcher

The CoreDispatcher that this object is associated with.

Remarks

A DependencyObject must be created on a UI thread. The Dispatcher property provides the CoreDispatcher that can be used to marshal calls to other threads, using RunAsync and an awaitable function. For more info on awaitable functions, see Quickstart: Calling asynchronous APIs.

This Dispatcher property gets the CoreDispatcher that is associated with the app UI thread. You can call this method on any DependencyObject that is part of the UI. You can also call it on a DependencyObject that is a source in a data binding scenario.

Window.Dispatcher also references the CoreDispatcher that is associated with the UI thread. Window.Dispatcher is basically just a wrapper around CoreWindow.Dispatcher so that a Window class has easy access to it.

Caution  The connection for a Dispatcher value is not available in a design-time view. This can cause issues if you've created a custom control that uses Dispatcher values and that code is accessed by a design-time environment through code paths that a design view uses, such as constructors and Loaded handlers. If you're writing a custom control and you encounter this issue, consider introducing a guard conditional in your code such as not calling that code when DesignModeEnabled is true.

Examples

This example shows a usage of Dispatcher for the implicit this of a code-behind file for a Page. This example uses a lambda expression to add the DispatchedHandler implementation. The handler itself is handling the Accelerometer.ReadingChanged event, which won't be executed on the UI thread.


        private async void ReadingChanged(object sender, AccelerometerReadingChangedEventArgs e)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                AccelerometerReading reading = e.Reading;
                ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
                ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
                ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
            });
        }

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml
Windows::UI::Xaml [C++]

Metadata

Windows.winmd

See also

DependencyObject

 

 

Show:
© 2014 Microsoft