Will a system clock adjust affect running the stopwatch in C#?

情到浓时终转凉″ 提交于 2020-01-03 08:48:58

问题


Let's say I have Stopwatch running in my code and in the middle, some other application has changed the system clock (I have Domain Time on my server syncing every second).

Will it affect the stopwatch result?


回答1:


No, internally Stopwatch relies on Win32 QueryPerformanceCounter() which is not tied to the system clock.




回答2:


It depends.

Stopwatch relies on Win32 QueryPerformanceCounter() only if it is available. This implementation is timechange tolerant.

In case high-resolution counters are not available, Stopwatch falls back to system timer, basically DateTime.UtcNow.Ticks call. In this unfortunate case Stopwatch results will be affected by time(not timezone) changes.

As it is written in cited MSDN article, you can check if high-resolution counters are available using IsHighResolution field.

You can blame .Net team using this won't fix defect :)




回答3:


No, it won't affect it.

From MSDN:

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.



来源:https://stackoverflow.com/questions/14079523/will-a-system-clock-adjust-affect-running-the-stopwatch-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!