Posted by: Zeeshan Amjad | October 10, 2011

Using DispatcherTimer class


There are already three different classes for Timer and WPF introduced one more class for it. The main reason is WPF architecture itself. We can’t use Timer class because it is running in another thread so it can’t update any UI element created by WPF application. To appropriately update the user interface, we need to get the dispatcher and then palace a request in its queue by using BeginInvoke method. But in WPF DispatcherTimer class does this for us and we don’t have to call the BeginInvoke method. It calls our method after specified amount of time just like other timer class. DispatcherTimer class is directly inherited by Object class and here is a class diagram of it.

DispatcherTimer

Here is a simple usage of this class.

Code Snippet
DispatcherTimer disTimer = new DispatcherTimer(DispatcherPriority.Normal);

disTimer.Interval = new TimeSpan(0, 0, 15);
disTimer.Tick += new EventHandler(disTimer_Tick);
disTimer.Start();

 

We created a simple application that has only one text box and with the help of this class we clear the content of that textbox after every 15 seconds. Here is a XAML of our program.

Code Snippet
<Window x:Class="WpfDispatcherTimer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
        Title="DispatcherTimer" Height="300" Width="400">
    <Grid>
        <TextBox Name="text" Margin="5" TextWrapping="Wrap" />
    </Grid>
</Window>

 

And here is complete C# code of our program.

Code Snippet
using System;
using System.Windows;
using System.Windows.Threading;

namespace WpfDispatcherTimer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DispatcherTimer disTimer = new DispatcherTimer(DispatcherPriority.Normal);

        public MainWindow()
        {
            InitializeComponent();

            disTimer.Interval = new TimeSpan(0, 0, 15);
            disTimer.Tick += new EventHandler(disTimer_Tick);
            disTimer.Start();
        }

        void disTimer_Tick(object sender, EventArgs e)
        {
            text.Text = "";            
        }
    }
}

 

This is an output of the program.

DispatcherTimerOutput

Advertisements

Responses

  1. Thanks, this really helped me! I was trying to update UI elements via another thread with Dispatcher.Invoke and was running into all types of problems (e.g., “The calling thread cannot access this object because a different thread owns it.”, “The calling thread must be STA, because many UI components require this.”, etc.) Many thanks, DOUG

    • Nice to hear that I am able to solve your problem. Thanks to visit my blog.

      Regards
      Zeeshan Amjad


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: