0
dispatcherTimer = new DispatcherTimer(DispatcherPriority.Normal); // or .Release
dispatcherTimer.Tick += f;
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(x);


 x => real time
11 => 15.9 ms
12 => 15.9 ms
13 => 17 ms
16 => 25 ms

Above measurements are taken with different combinations of DispatcherPriority.Render and DispatcherPriority.Normal, Debug build and release. Results are almost identical.
f is fired on average every 25ms for x = 16. Why?

I have read MS explanation:

Remarks
Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs.

But that doesn't explain time shift or 11 => 15.9 or 16 => 25.

CoR
  • 3,826
  • 5
  • 35
  • 42
  • It does explain 11 and 15.9. 15.9 happened after 11 ms passed, which meets the contract 'guaranteed to not execute before the time interval occurs'. – Rob Dec 19 '16 at 23:01
  • The timer just triggers a handler that will be handled when able to. If the CPU is handling other tasks at the same time, or is busy with many tasks, it will take it some time to get to the job. if real time is a necessity, it is likely you will need to also include a real time clock ticks within your handler. – vipersassassin Dec 19 '16 at 23:04
  • @Rob but time shift is almost like a constant. It's 4ms for required 11,12,13ms and 9ms for desired 16ms. Why? – CoR Dec 19 '16 at 23:06
  • 1
    @CoR Not sure what you mean by constant, there doesn't seem to be any relationship between the values at all. They're all close together because 1ms is likely under the resolution offered by the timer. You might find better results testing 100ms, 200ms, 250ms, etc. – Rob Dec 19 '16 at 23:08
  • Aaaaaaaaa, that makes sense! For 11, 12, 13, 14, 15 timer gives approximately 16ms of calling time. But for 16 and above it jumps to next resolutin value wish is 25ms! – CoR Dec 19 '16 at 23:12
  • System clock resolution is on the order of 15 ms. You can't get timer results more accurate than that normally. Also since this a dispatcher timer, results can be delayed by WPF preempting for higher priority work. – Mike Zboray Dec 20 '16 at 00:16
  • I understand completely. Thanks. – CoR Dec 20 '16 at 08:15

0 Answers0