Stopwatch Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Provides a set of methods and properties that you can use to accurately measure elapsed time.

To browse the .NET Framework source code for this type, see the Reference Source.

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

System.Object
  System.Diagnostics.Stopwatch

type Stopwatch = class end

NameDescription
System_CAPS_pubmethodStopwatch()

Initializes a new instance of the Stopwatch class.

NameDescription
System_CAPS_pubpropertyElapsed

Gets the total elapsed time measured by the current instance.

System_CAPS_pubpropertyElapsedMilliseconds

Gets the total elapsed time measured by the current instance, in milliseconds.

System_CAPS_pubpropertyElapsedTicks

Gets the total elapsed time measured by the current instance, in timer ticks.

System_CAPS_pubpropertyIsRunning

Gets a value indicating whether the Stopwatch timer is running.

NameDescription
System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticGetTimestamp()

Gets the current number of ticks in the timer mechanism.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodReset()

Stops time interval measurement and resets the elapsed time to zero.

System_CAPS_pubmethodRestart()

Stops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time.

System_CAPS_pubmethodStart()

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

System_CAPS_pubmethodSystem_CAPS_staticStartNew()

Initializes a new Stopwatch instance, sets the elapsed time property to zero, and starts measuring elapsed time.

System_CAPS_pubmethodStop()

Stops measuring elapsed time for an interval.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticFrequency

Gets the frequency of the timer as the number of ticks per second. This field is read-only.

System_CAPS_pubfieldSystem_CAPS_staticIsHighResolution

Indicates whether the timer is based on a high-resolution performance counter. This field is read-only.

System_CAPS_noteNote

To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.

A Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals. 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.

A Stopwatch instance is either running or stopped; use IsRunning to determine the current state of a Stopwatch. Use Start to begin measuring elapsed time; use Stop to stop measuring elapsed time. Query the elapsed time value through the properties Elapsed, ElapsedMilliseconds, or ElapsedTicks. You can query the elapsed time properties while the instance is running or stopped. The elapsed time properties steadily increase while the Stopwatch is running; they remain constant when the instance is stopped.

By default, the elapsed time value of a Stopwatch instance equals the total of all measured time intervals. Each call to Start begins counting at the cumulative elapsed time; each call to Stop ends the current interval measurement and freezes the cumulative elapsed time value. Use the Reset method to clear the cumulative elapsed time in an existing Stopwatch instance.

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation.

The Stopwatch class assists the manipulation of timing-related performance counters within managed code. Specifically, the Frequency field and GetTimestamp method can be used in place of the unmanaged Win32 APIs QueryPerformanceFrequency and QueryPerformanceCounter.

System_CAPS_noteNote

On a multiprocessor computer, it does not matter which processor the thread runs on. However, because of bugs in the BIOS or the Hardware Abstraction Layer (HAL), you can get different timing results on different processors. To specify processor affinity for a thread, use the ProcessThread.ProcessorAffinity method.

The following example demonstrates how to use the Stopwatch class to determine the execution time for an application.

No code example is currently available or this language may not be supported.

The following example demonstrates the use of the Stopwatch class to calculate performance data.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: