See Also: Stopwatch Members
A System.Diagnostics.Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals. In a typical System.Diagnostics.Stopwatch scenario, you call the Stopwatch.Start method, then eventually call the Stopwatch.Stop method, and then you check elapsed time using the Stopwatch.Elapsed property.
A System.Diagnostics.Stopwatch instance is either running or stopped; use Stopwatch.IsRunning to determine the current state of a System.Diagnostics.Stopwatch. Use Stopwatch.Start to begin measuring elapsed time; use Stopwatch.Stop to stop measuring elapsed time. Query the elapsed time value through the properties Stopwatch.Elapsed, Stopwatch.ElapsedMilliseconds, or Stopwatch.ElapsedTicks. You can query the elapsed time properties while the instance is running or stopped. The elapsed time properties steadily increase while the System.Diagnostics.Stopwatch is running; they remain constant when the instance is stopped.
By default, the elapsed time value of a System.Diagnostics.Stopwatch instance equals the total of all measured time intervals. Each call to Stopwatch.Start begins counting at the cumulative elapsed time; each call to Stopwatch.Stop ends the current interval measurement and freezes the cumulative elapsed time value. Use the Stopwatch.Reset method to clear the cumulative elapsed time in an existing System.Diagnostics.Stopwatch instance.
The System.Diagnostics.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 System.Diagnostics.Stopwatch class uses that counter to measure elapsed time. Otherwise, the System.Diagnostics.Stopwatch class uses the system timer to measure elapsed time. Use the Stopwatch.Frequency and Stopwatch.IsHighResolution fields to determine the precision and resolution of the System.Diagnostics.Stopwatch timing implementation.
The System.Diagnostics.Stopwatch class assists the manipulation of timing-related performance counters within managed code. Specifically, the Stopwatch.Frequency field and Stopwatch.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.