Before reading this article, I highly recommend reading the previous parts of the series.
- Learn Universal Windows Programming Via Modern C++
- Learn Universal Windows Programming Via Modern C++ (Button Control)
- Learn Universal Windows Programming Via Modern C++ (Stackpanel)
- Learn Universal Windows Programming Via Modern C++ (CheckBox)
- Learn Universal Windows Programming Via Modern C++ (RadioButton)
- Learn Universal Windows Programming Via Modern C++ (Combobox)
- Learn Universal Windows Programming Via Modern C++ (Border)
- Learn Universal Windows Programming Via Modern C++ (CommandBar)
- Learn Universal Windows Programming Via Modern C++ (SplitView Control)
- Learn Universal Windows Programming Via Modern C++ (AutoSuggestBox)
- Learn Universal Windows Programming Via Modern C++ (ContentDialog)
- Learn Universal Windows Programming Via Modern C++ (Grid control)
- Learn Universal Windows Programming Via Modern C++ (RelativePanel)
- Learn Universal Windows Programming Via Modern C++ (ProgressBar)
DispatcherTimer
DispatcherTimer is a timer control that is integrated into the Dispatcher queue. A Timer event is called in a specific time interval. The main advantage of a DispatcherTimer is that it can run the code on the same UI thread.
Let us see how to implement this feature and important properties.
Interval - How frequently an event should be triggered (i.e. amount of time between ticks)
TimeSpan as an input for the Interval - Actually, it is std::Chrono in C++ ( C++/WinRT is typecast into the TimeSpan type).
Given below is the sample code how to define the TimeSpan ( It's specified as1 second).
- DispatcherTimer dispatchTimer;
- std::chrono::seconds sec(1);
- dispatchTimer.Interval(sec);
- dispatchTimer.Start();
Tick
Occurs when the timer interval has elapsed (based on the interval set by the user).
- DispatcherTimer dispatchTimer;
- dispatchTimer.Tick({ this,&App::dispatch_tick });
- void dispatch_tick(IInspectable const & sender, Windows::Foundation::IUnknown const& from)
- {
- }
Start
Start function is used to start the Timer. If the timer is already running, call the start method because it restarts the process.
- DispatcherTimer dispatchTimer;
- dispatchTimer.Start();
Stop
Stop function is used to stop the time if it is running.
- DispatcherTimer dispatchTimer;
- dispatchTimer.Stop();

Join the conversation! Your thoughts help the community grow.