Stopwatch Class

Stopwatch Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

System::Object
  System.Diagnostics::Stopwatch

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

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

The Stopwatch type exposes the following members.

  NameDescription
Public methodStopwatchCreates a new instance of the Stopwatch class.
Top

  NameDescription
Public propertyElapsedGets the total elapsed time measured by the current instance.
Public propertyElapsedMillisecondsGets the total elapsed time measured by the current instance, in milliseconds.
Public propertyElapsedTicksGets the total elapsed time measured by the current instance, in timer ticks.
Public propertyIsRunningGets a value indicating whether the Stopwatch timer is running.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetElapsedDateTimeTicksGets the number of ticks that represent the date and time of this instance.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetRawElapsedTicksGets the total elapsed time measured by the current instance, in timer ticks.
Public methodStatic memberGetTimestampGets the current number of ticks in the timer mechanism.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodResetStops time interval measurement and resets the elapsed time to zero.
Public methodRestartStops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time.
Public methodStartStarts, or resumes, measuring elapsed time for an interval.
Public methodStatic memberStartNewInitializes a new Stopwatch instance, sets the elapsed time property to zero, and starts measuring elapsed time.
Public methodStopStops measuring elapsed time for an interval.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public fieldStatic memberFrequencyGets the frequency of the timer as the number of ticks per second. This field is read-only.
Public fieldStatic memberIsHighResolutionIndicates whether the timer is based on a high-resolution performance counter. This field is read-only.
Top

A Stopwatch instance can measure the elapsed time for one interval or the total amount 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 the 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.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

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

Show:
© 2017 Microsoft