Implement timers in Windows Phone
This article explains how to implement a timer (DispatcherTimer) in Windows Phone.
Article Metadata
Tested with
Devices(s): Windows Phone Emulator
Compatibility
Platform(s): Windows Phone 7.5, 8
Article
Keywords: dispatchertimer, windows phone
Created: girishpadia
(13 Oct 2011)
Last edited: hamishwillee
(10 Apr 2013)
Contents |
Introduction
The code example shows how to use DispatcherTimer on Windows Phone. Timers are very useful for executing code at specified time intervals.
Implementation
The code in this article is written using C#. A text box will be used to display a clock, which is refreshed every second. Follow the below steps to create the clock app:
- Create a new "Silverlight" project using C# language. Here we are naming the project as "Clock"
- Place a text box and a button.
- We are giving textbox name as "txtClock". We can also change the font and color of content in the text box.
- We will now add following code to the project.
using System;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
namespace Clock
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
void OnTimerTick(Object sender, EventArgs args)
{
// text box property is set to current system date.
// ToString() converts the datetime value into text
txtClock.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
// creating timer instance
DispatcherTimer newTimer = new DispatcherTimer();
// timer interval specified as 1 second
newTimer.Interval = TimeSpan.FromSeconds(1);
// Sub-routine OnTimerTick will be called at every 1 second
newTimer.Tick += OnTimerTick;
// starting the timer
newTimer.Start();
}
}
}
Building and Running
The application is now ready to be built (Ctrl+Shift+B) and ran (Ctrl+F5) either on the emulator or a device.
Note
This application has been tested on the emulator, and should work on a physical device.


Contents
Atom217 - alternatively
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using System.Windows.Threading;
namespace Timer {
public partial class MainPage : PhoneApplicationPage { DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(.1)}; // Constructor public MainPage() { InitializeComponent(); this.timer.Tick+=new EventHandler(timer_Tick); this.Loaded += new RoutedEventHandler(timer_Tick); } private void timer_Tick(object sender, EventArgs e) { output.Text = DateTime.Now.ToLongTimeString(); } }}
output is the name of the textblockMainPagexaml .cs
atom217 00:56, 25 March 2012 (EET)
Hamishwillee - Why this timer
Windows Phone has a number of different timers. It would be useful to explain why you're using this one, and its relative benefits/downsides when compared to other ones.
Hamishwillee - Thanks for the fixes
Thanks for the tidy up Matthew (and Chintan). Definitely an improvement.hamishwillee 06:57, 11 February 2013 (EET)
Jweide - good example
Thank you, this was just what I needed.
Jokkejweide 15:24, 6 April 2013 (EEST)