Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Stopwatch Class
Stopwatch Methods
 Start Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Stopwatch..::.Start Method

Updated: August 2008

Starts, or resumes, measuring elapsed time for an interval.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Sub Start
Visual Basic (Usage)
Dim instance As Stopwatch

instance.Start()
C#
public void Start()
Visual C++
public:
void Start()
JScript
public function Start()

In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.

Once started, a Stopwatch timer measures the current interval, in elapsed timer ticks, until the instance is stopped or reset. Starting a Stopwatch that is already running does not change the timer state or reset the elapsed time properties.

When a Stopwatch instance measures more than one interval, the Start method resumes measuring time from the current elapsed time value. A Stopwatch instance calculates and retains the cumulative elapsed time across multiple time intervals, until the instance is reset. Use the Reset method before calling Start to clear the cumulative elapsed time in a Stopwatch instance.

The following example demonstrates how to use the Start method to start a timer that measures the execution time of an application.

Visual Basic
Imports System
Imports System.Diagnostics
Imports System.Threading


Class Program

    Shared Sub Main(ByVal args() As String)
        Dim stopWatch As New Stopwatch()
        stopWatch.Start()
        Thread.Sleep(10000)
        stopWatch.Stop()
        ' Get the elapsed time as a TimeSpan value.
        Dim ts As TimeSpan = stopWatch.Elapsed

        ' Format and display the TimeSpan value.
        Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
        Console.WriteLine(elapsedTime, "RunTime")

    End Sub 'Main
End Class 'Program
C#
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        Thread.Sleep(10000);
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine(elapsedTime, "RunTime");
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

August 2008

Updated the example.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
StopWatch Sample, recoded in PowerShell      Thomas Lee   |   Edit   |   Show History
<# 
.SYNOPSIS 
    Demonstrates use of the System.Diagnostics.Stopwatch .NET Class   
.DESCRIPTION 
    This script is a community content MSDN sample,using PowerShell 
.NOTES 
    File Name  : Get-ElapsedTime
    Author     : Thomas Lee - tfl@psp.co.uk 
    Requires   : PowerShell V2 CTP3 
.LINK 
    Sample posted to: 
    http://pshscripts.blogspot.com 
    Original MSDN sample at: 
    http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx
.EXAMPLE 
    PSH [C:\foo]: .\get-stopwatch.ps1'
    Runtime = 00:00:10.03
#> 
##
# start of script
##
# Create a new stopwatch objecty
$StopWatch = New-Object system.Diagnostics.Stopwatch

# Start the stop watch
$stopWatch.Start()

# Go to sleep for approx 10 seconds
[system.threading.Thread]::Sleep(10000)
  

# Now after sleep, stop the watch
$StopWatch.Stop();
# Get the elapsed time as a TimeSpan value.
$ts = $StopWatch.Elapsed
# Format and display the TimeSpan value.
$ElapsedTime = [system.String]::Format("{0:00}:{1:00}:{2:00}.{3:00}",
$ts.Hours, $ts.Minutes, $ts.Seconds,
$ts.Milliseconds / 10);
"Runtime = $elapsedTime"
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker