SIGN UP MEMBER LOGIN:    
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.  

share this article :
post comment
 

hey i saw the sample but it is not i wanted. I do not want the timer to be open in another window when i run the program.How do i actually add it into my game without displaying the countdown of the time? Like those normal game, i want to display it at the top right hand corner and from 60seconds counting down to 0 second. can u help?

Posted by Nobody Nov 11, 2011

Its a very simple and good article for those who try to implement timers in there WPF project.. thanks a lot..

Posted by Jisss shyam Aug 27, 2011

Is your application is a WPF application? What version of Visual Studio are you using? Make sure when you create a new project, it is a WPF application.

Posted by Mahesh Chand Nov 07, 2010

I get this error when I try to compile:
Error    1    The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxx'.    C:\Users\xxxx\Downloads\DispatchTimerSampleInCS\DispatchTimerSampleInCS\DispatchTimerSampleInCS\App.xaml.cs    13    26    DispatchTimerSampleInCS

Posted by jay aigner Nov 01, 2010

Mahesh thank you for al this samples, i have learnt a lot.
do you have more video whit samples.
Keep up the good working.

Greetings
Abdel Manti
abdelmanti@telenet.be

Posted by Abdel Manti Feb 10, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor