Implementing the .NET DispatcherTimer

Standard
Share

One of the issues with multi-threading in WPF is the inability for processes on a separate thread from the user interface to directly access objects in the UI. The .NET Timer object does not run in the same thread as WPF’s UI, so it therefore cannot directly update the UI. Instead, the Timer must post UI updates to the dispatcher of the UI thread, using the Invoke or BeginInvoke methods. However, an easier method exists for implementing a Timer that updates the UI – the DispatcherTimer.

The DispatcherTimer runs on the same thread as the UI. Therefore, using the DispatcherTimer instead of the traditional Timer object allows UI updating without the need for the Invoke or BeginInvoke methods.

The DispatcherTimer works in almost the same way as the traditional Timer:

  1. Create an instance of the DispatcherTimer
  2. Assign a TimeSpan to the DispatcherTimer’s Interval property
  3. Assign an event handler to the DispatcherTimer’s Tick event
  4. Start the timer

It really is that easy! Let’s take a look at some sample code. All of the following code can be placed in your DataModel.

...
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 1);
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
...

private void dt_Tick(object sender, EventArgs e)
{
    // Code to run every dt.Interval goes here...
    // perhaps updating a "clock" label on the UI...
    // were this a traditional Timer, we couldn't do this directly:
    ClockValue = DateTime.Now();
}

As I do with all my articles, let’s break this down:

DispatcherTimer dt = new DispatcherTimer();

Here, we’re simply creating an instance of the DispatcherTimer object and calling it dt.

dt.Interval = new TimeSpan(0, 0, 1);

In this line, we’re creating a TimeSpan instance using one of its multiple constructors. This one creates a TimeSpan object of one second, and assigns it to the DispatcherTimer’s Interval property. What we’re saying here is that the DispatcherTimer’s Tick event will fire every second.

dt.Tick += new EventHandler(dt_Tick);

In line #4, we’re simply telling the DispatcherTimer which method, or event handler, to run when it fires its Tick event.

dt.Start();

Here, we’re telling the DispatcherTimer to start firing Tick events. At this point, the code specified in the event handler dt_Tick will run every second.

private void dt_Tick(object sender, EventArgs e)
{
    // Code to run every dt.Interval goes here...
    // perhaps updating a "clock" label on the UI...
    // were this a traditional Timer, we couldn't do this directly:
    ClockValue = DateTime.Now();
}

Line #8 starts the event handler for the Tick event (assigned in line #4). There’s really only one line of code here – line #13 – where we’re setting our DataModel’s ClockValue DateTime property to the current time.

It really is that straightforward. DispatcherTimer gives us a Timer-like object with the traditional Timer’s Tick event, but one that can directly update the UI.

2 thoughts on “Implementing the .NET DispatcherTimer

Leave a Reply

Your email address will not be published. Required fields are marked *