Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The value of the system clock that <code>DateTime.Now</code> reads is only updated every 15 ms or so (or 10 ms on some systems), which is why the times are quantized around those intervals. There is an additional quantization effect that results from the fact that your code is running in a multithreaded OS, and thus there are stretches where your application is not "alive" and is thus not measuring the real current time.</p> <p>Since you're looking for an ultra-accurate time stamp value (as opposed to just timing an arbitrary duration), the <code>Stopwatch</code> class by itself will not do what you need. I think you would have to do this yourself with a sort of <code>DateTime</code>/<code>Stopwatch</code> hybrid. When your application starts, you would store the current <code>DateTime.UtcNow</code> value (i.e. the crude-resolution time when your application starts) and then also start a <code>Stopwatch</code> object, like this:</p> <pre><code>DateTime _starttime = DateTime.UtcNow; Stopwatch _stopwatch = Stopwatch.StartNew(); </code></pre> <p>Then, whenever you need a high-resolution <code>DateTime</code> value, you would get it like this:</p> <pre><code>DateTime highresDT = _starttime.AddTicks(_stopwatch.Elapsed.Ticks); </code></pre> <p>You also may want to periodically reset _starttime and _stopwatch, to keep the resulting time from getting too far out of sync with the system time (although I'm not sure this would actually happen, and it would take a long time to happen anyway).</p> <p><strong>Update</strong>: since it appears that Stopwatch <em>does</em> get out of sync with the system time (by as much as half a second per hour), I think it makes sense to reset the hybrid <code>DateTime</code> class based on the amount of time that passes between calls to check the time:</p> <pre><code>public class HiResDateTime { private static DateTime _startTime; private static Stopwatch _stopWatch = null; private static TimeSpan _maxIdle = TimeSpan.FromSeconds(10); public static DateTime UtcNow { get { if ((_stopWatch == null) || (_startTime.Add(_maxIdle) &lt; DateTime.UtcNow)) { Reset(); } return _startTime.AddTicks(_stopWatch.Elapsed.Ticks); } } private static void Reset() { _startTime = DateTime.UtcNow; _stopWatch = Stopwatch.StartNew(); } } </code></pre> <p>If you reset the hybrid timer at some regular interval (say every hour or something), you run the risk of setting the time back before the last read time, kind of like a miniature Daylight Savings Time problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload