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.
Assembly: System (in System.dll)
Thetype exposes the following members.
|Elapsed||Gets the total elapsed time measured by the current instance.|
|ElapsedMilliseconds||Gets the total elapsed time measured by the current instance, in milliseconds.|
|ElapsedTicks||Gets the total elapsed time measured by the current instance, in timer ticks.|
|IsRunning||Gets a value indicating whether the timer is running.|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetElapsedDateTimeTicks||Gets the high-resolution ticks as DateTime.Ticks if the system is using a high-resolution counter.|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetRawElapsedTicks||Gets the total elapsed time, including the time since was last started.|
|GetTimestamp||Gets the current number of ticks in the timer mechanism.|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|Reset||Stops time interval measurement and resets the elapsed time to zero.|
|Restart||Stops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time.|
|Start||Starts, or resumes, measuring elapsed time for an interval.|
|StartNew||Initializes a new instance, sets the elapsed time property to zero, and starts measuring elapsed time.|
|Stop||Stops measuring elapsed time for an interval.|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
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 instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals. In a typical scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.
A instance is either running or stopped; use IsRunning to determine the current state of a . 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 is running; they remain constant when the instance is stopped.
By default, the elapsed time value of a 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 instance.
The 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 class uses that counter to measure elapsed time. Otherwise, the class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the timing implementation.
The 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.
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 class to determine the execution time for an application.
The following example demonstrates the use of the class to calculate performance data.
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
XNA FrameworkSupported in: 3.0, 2.0, 1.0
Portable Class LibrarySupported in: Portable Class Library
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8