Stop Watch using Windows Phone
This article explains how to create stop watch in Windows Phone. It extends the article: Implement Timer control in Windows Phone.
Article Metadata
Code Example
Source file: Media:StopWatch.zip
Tested with
Devices(s): Windows Phone Emulator
Compatibility
Platform(s): Windows Phone 7.5, 8
Article
Keywords: DispatcherTimer , Stop watch
Created: girishpadia
(13 Oct 2011)
Last edited: hamishwillee
(10 Apr 2013)
Introduction
This code example creates a simple stop watch which you can start and stop using the buttons Start Clock and Stop respectively. The example uses DispatcherTimer to update the stopwatch every second and lists how long the timer ran (calculated from current DateTime when the buttons are pressed).
Implementation
- Create a new "Silverlight" project using C# and name the project as "StopWatch".
- Drag one textblock from the toolbox and name it txtClock
- Drag two buttons and place them just below the txtClock as shown in the application image below. Name the buttons btnStart and btnStop respectively.
- In the XAML file, add the event handlers for the newly created buttons by adding the attribute in the btnStart tag and
Click="btnStart_Click"
in the btnStop tag. Alternatively, just double click both buttons in the layout view.Click="btnStop_Click"
- Drag another textblock and name it lblTimer and place it below the buttons.
- Copy and paste following code into your application.
using System;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
namespace StopWatch
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
DateTime lastTime,startTime;
public MainPage()
{
InitializeComponent();
}
void OnTimerTick(Object sender, EventArgs args)
{
txtClock.Text = DateTime.Now.ToString();
}
private void btnStart_Click(object sender, System.Windows.RoutedEventArgs e)
{
DispatcherTimer newTimer = new DispatcherTimer();
newTimer.Interval = TimeSpan.FromSeconds(1);
newTimer.Tick += OnTimerTick;
newTimer.Start();
lastTime = DateTime.Now;
startTime = DateTime.Now;
lblTimer.Text = "Start time : " + lastTime.ToString() + "\n";
}
private void btnStop_Click(object sender, System.Windows.RoutedEventArgs e)
{
DateTime endTime = DateTime.Now;
TimeSpan span = endTime.Subtract(startTime);
lblTimer.Text += "Seconds from begining: "+span.TotalSeconds.ToString()+"\n";
span = endTime.Subtract(lastTime);
lblTimer.Text += "Seconds from last stop: " + span.TotalSeconds.ToString() + "\n\n";
lastTime = DateTime.Now;
}
}
}
Tested On
The application is tested on Windows Phone Emulator.



Hamishwillee - I've subedited
I've subedited and updated this - adding links to the DispatcherTimer. One question, why this type of timer, and not one of the others?hamishwillee 07:42, 27 April 2012 (EEST)
Mihasi - Metadata update needed
I've made some changes to the code, so the metadata should be updated. Not sure how to do this, new to Windows Phone and this wiki.Mihasi 13:34, 8 July 2012 (EEST)
Hamishwillee - @Mihasi - thank you
Hi Mihasi
Welcome to the wiki. Your profile is not public so can't find anything about you - are you community or Nokia internal?
Thanks for the change to code - they look good.
In terms of the metadata the usage is documented in Template:ArticleMetaData. In general what we're trying to do with most of the metadata is indicate the likely "relevance" of the content so that down the track people can tell if its likely still to be useful and accurate over time - so we show review timestamps for articles if they are still relevant after a year or so. We add update stamps if we make big changes (effectively we're "renewing" the creation date). We add devices and platforms that the article has been tested on as they come along. Upshot is that if someone sees an article with new SDKs, devices and stamps they know its relevant. If its older ones, they know that its not as "trustable".
In this case
You wouldn't update SDK or platform information, because that is still current.
Hope that makes sense.
Thanks again for this - every little bit helps.
regards
Hamishhamishwillee 10:06, 10 July 2012 (EEST)