ARTICLE

Timer in WPF

Posted by Mahesh Chand Articles | WPF September 30, 2009
This article demonstrates how to implement timer in WPF using the DispatchTimer class.
Reader Level:
Download Files:
 

This article demonstrates how to implement timer in WPF using the DispatchTimer class.

In this article and the attached project, I am going to create a WPF application that has a ListBox control and this control is being updated every second with current time.  The application looks like Figure 1.

WpfTimerImg1.gif

Figure 1

Creating a DispatchTimer

XAML does not support any timer feature and WPF does not have a Timer control or class. The DispatchTimer class defined in the System.Windows.Threading namespace is used to add timer functionality in WPF.

The following code snippet creates a DispatchTimer object.

DispatcherTimer dispatcherTimer = new DispatcherTimer();

 

Setting Tick and Interval

The Tick event handler executes when a DispatchTimer is started on a given Interval. The following code snippet sets the Tick and Interval of a DispatchTimer.

    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

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

 

Start DispatchTimer

The Start method is used to start a DispatchTimer.

    dispatcherTimer.Start();

 

Complete Code Example

The code snippet in Listing 1 creates a DispatchTimer, sets its Tick event and Interval property and calls its Start method. The Start method starts the timer and the Tick event handler is executed on the given Interval value. In this code, on the Tick event handler, I update a ListBox control and add the current time. I also set the selected item of the ListBox to the currently added item and make sure this item is visible in the ListView.

 

private void Window_Loaded(object sender, RoutedEventArgs e)

{

    DispatcherTimer dispatcherTimer = new DispatcherTimer();

    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

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

    dispatcherTimer.Start();

}

 

private void dispatcherTimer_Tick(object sender, EventArgs e)

{

    listBox1.Items.Add(DateTime.Now.Hour.ToString() + ":" +

        DateTime.Now.Second.ToString());

   

    CommandManager.InvalidateRequerySuggested();

    listBox1.Items.MoveCurrentToLast();

    listBox1.SelectedItem = listBox1.Items.CurrentItem;

    listBox1.ScrollIntoView(listBox1.Items.CurrentItem);

}

Listing 1

Summary

In this article, I discussed how we can create a DispatchTimer control to build timer applications in WPF and C#.  We saw, how to update a ListBox control every second with the current time.  

Login to add your contents and source code to this article
post comment
     

that's great! by the way, your code [ listBox1.Items.Add(DateTime.Now.Hour.ToString() + ":" +DateTime.Now.Second.ToString()); ] should be changed to [ listBox1.Items.Add(DateTime.Now.Minute.ToString("D2") + ":" + DateTime.Now.Second.ToString("D2")); ]

Posted by Sun Jan 09, 2013

It doesn't work !

Posted by Amine Nov 28, 2012

Helped me.

Posted by Amol Vitekar Sep 03, 2012

You have been such a big help to me!...I'm just a beginner learning by myself (15 years old)... Thank you sooo much for your help!

Posted by mma Aug 06, 2012

good one

Posted by Shuvro Pall Jul 12, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter