Skip to content

Latest commit

 

History

History
70 lines (54 loc) · 4.02 KB

dispatchertimer.md

File metadata and controls

70 lines (54 loc) · 4.02 KB
-api-id -api-type
T:Windows.UI.Xaml.DispatcherTimer
winrt class

Windows.UI.Xaml.DispatcherTimer

-description

Provides a timer that is integrated into the Dispatcher queue, which is processed at a specified interval of time and at a specified priority.

-remarks

The DispatcherTimer can be used to run code on the same thread that produces the UI thread. Code running on this thread has the privilege to create and modify objects that can only be created and modified on the UI thread. To specify that code should run on the UI thread, set the Interval property and then call the Start method. The Tick event fires after the time specified in Interval has elapsed. Tick continues firing at the same Interval until the Stop method is called, the app terminates, or the app is suspended (fires Suspending).

One scenario for DispatcherTimer is to check properties on sensors where changes to the sensor values are not purely event-driven, or the events don't give you the granularity you want. You can see this in the Accelerometer sample.

Other scenarios for DispatcherTimer include checking for state changes that don't have related events, or for periodic UI updates that can't use a storyboarded animation or a two-way binding.

Tip

If you're migrating Microsoft Silverlight or Windows Presentation Foundation (WPF) code, the DispatcherTimer and the related Dispatcher was in a separate System.Windows.Threading namespace. There is no Windows.UI.Xaml.Threading namespace in the Windows Runtime, so this class is in Windows.UI.Xaml.

If you aren't doing anything with the UI thread in your Tick handlers and just need a timer, you could also use ThreadPoolTimer instead. Also, for techniques like ThreadPoolTimer or a .NET Task, you aren't totally isolated from the UI thread. You could still assign to the UI thread asynchronously using CoreDispatcher.RunAsync.

-examples

This example code implements a simple console-style timer that writes data to a TextBlock named TimerLog (XAML that defines TimerLog is not shown). The Interval value is set to 1, and the log displays the actual elapsed time for each Tick.

[!code-csharp1]

// MainPage.cpp
...
#include <chrono>
...
void MainPage::StartTimerAndRegisterHandler()
{
    Windows::UI::Xaml::DispatcherTimer timer;
    timer.Interval(std::chrono::milliseconds{ 500 });
    auto registrationtoken = timer.Tick({this, &MainPage::OnTick });
    timer.Start();
}

void MainPage::OnTick(Windows::Foundation::IInspectable const& /* sender */,
    Windows::Foundation::IInspectable const& /* e */)
{
    // do something on each tick here ...
}
// .cpp definition, .h not shown
void MainPage::StartTimerAndRegisterHandler() {
    auto timer = ref new Windows::UI::Xaml::DispatcherTimer();
    TimeSpan ts;
    ts.Duration = 500;
    timer->Interval = ts;
    timer->Start();
    auto registrationtoken = timer->Tick += ref new EventHandler<Object^>(this,&MainPage::OnTick);
}
void MainPage::OnTick(Object^ sender, Object^ e) {
    // do something on each tick here ...
}

-see-also

CoreDispatcher, ThreadPoolTimer