.NET Framework Class Library
Stopwatch..::.Stop Method

Updated: August 2008

Stops measuring elapsed time for an interval.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)
Syntax

Visual Basic (Declaration)
Public Sub Stop
Visual Basic (Usage)
Dim instance As Stopwatch

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

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.

The Stop method ends the current time interval measurement. Stopping a Stopwatch that is not running does not change the timer state or reset the elapsed time properties.

When a Stopwatch instance measures more than one interval, the Stop method is equivalent to pausing the elapsed time measurement. A subsequent call to Start resumes measuring time from the current elapsed time value. Use the Reset method to clear the cumulative elapsed time in a Stopwatch instance.

Examples

The following example demonstrates how to use the Stop method to stop 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");
    }
}
Platforms

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.
Version Information

.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
See Also

Reference

Change History

Date

History

Reason

August 2008

Updated the example.

Customer feedback.

Tags :


Community Content

Thomas Lee
Stop Method sample, using PowerShell
<# 
.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"


Page view tracker