Export (0) Print
Expand All

Stopwatch.Stop Method

Stops measuring elapsed time for an interval.

Namespace:  System.Diagnostics
Assemblies:   System (in System.dll)
  System.Runtime.Extensions (in System.Runtime.Extensions.dll)

public void Stop()

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.

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

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("RunTime " + elapsedTime);
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft