-4

Im using visual c# wpf application and want to squeeze the most out of my timer.

Im currently using System.Timers.Timer, calling it with 1ms interval (and yes, i know i cant get 1ms timer using VS-wpf).

Im also measuring the timer performance using a Stopwatch, and seeing roughly 13ms.

Two questions:

  1. If the application changes from x86 to 64, should my timer performance increase?
  2. Is there a fast way to decrease timer interval (assuming i dont wanna switch to a real-time OS)

Thanks!

  • 1
    Decrease timer interval **below** 1ms intervals ? – Koby Douek Aug 27 '17 at 06:34
  • Any of the answers here apply to you? https://stackoverflow.com/questions/9228313/most-accurate-timer-in-net – Caius Jard Aug 27 '17 at 06:35
  • Maybe you should tell us what you _are trying to do_ first that requires an accurate timer? If the intent is for _smooth animations_ then you should really be using WPF-built-in `Storyboard`s than using say a WinForms approach – MickyD Aug 27 '17 at 06:35
  • I dont want to decrease below 1ms. Right now im getting 13ms. Im not trying to do animations, just communicating with a device. – Shahar Levy Aug 27 '17 at 08:18

1 Answers1

1

Answer to first question: No

Answer (two both questions):

The resolution for a System.Timers.Timer is the same as the system clock (see Accurate Windows timer? System.Timers.Timer() is limited to 15 msec), so creating it with values like 1 msec is useless.Apart from that, when a System.Timers.Timer fires an event, the event handler is typically scheduled on a ThreadPool thread. When that event handler is actually executed depends on the scheduler and the circumstances. So you cannot know how long your event handler will be lagging the actual firing of the event.

In Windows measuring time with fine resolution is possible (see other remarks). Having a piece of code executed precisely at intervals with msec resolution and at deterministic response times is not possible. In my opinion you really need a real time OS for that.

Clemens
  • 117,112
  • 10
  • 139
  • 247
Johan Donne
  • 2,518
  • 11
  • 20
  • Like i said, i know i wont achieve 1ms accuracy. But im trying to get the best performance i can from the visual studio timer. – Shahar Levy Aug 27 '17 at 08:21
  • 2
    And what does 'best performance' mean? If you mean 'shorter intervals than the 13 msec' you are already getting, the answer is 'No'. Even if you would write your own Timer class, you would still be limited by the scheduling of the Timer events. – Johan Donne Aug 27 '17 at 08:33
  • Meaning im at the minimal timer interval possible already, and any attempt to shorten it is futile? – Shahar Levy Aug 27 '17 at 08:46
  • 2
    About as futile as keeping asking the same question after having received an answer multipe times... – Johan Donne Aug 27 '17 at 09:21
  • @ShaharLevy Johan is correct. You don't seem to be paying attention with regards to the system clock and this particular timer class – MickyD Aug 28 '17 at 06:06
  • @JohanDonne love your last comment. Lol – MickyD Aug 28 '17 at 06:07