Using a timer to periodically update the UI
It is often require to update a portion of the user interface periodically. In that case, a timer object is beneficial to keep the UI refreshed. For example, in your application, you may want to show the current time at some part of the UI. For this, you can use a timer to periodically update the UI without the need to create a different thread.
The System.Windows.Threading.DispatcherTimer
class can be used to integrate into the Dispatcher
queue, and can process at a specified interval of time and at a specified priority.
In this recipe, we will use the DispatcherTimer
class to implement a timer, which will execute its subscribed Tick
event each time the specified Interval
is met.
Getting ready
Open Visual Studio and create a new WPF application project. Name it CH10.DispatcherTimerDemo
.
How to do it...
Follow these steps to create a digital clock experience with a timer:
- From
Solution Explorer
, navigate to theMainWindow.xaml
page. - Divide the default...