Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Why are timer frequencies extremely off?
    primarykey
    data
    text
    <p>Both <code>System.Timers.Timer</code> and <code>System.Threading.Timer</code> fire at intervals that are considerable different from the requested ones. For example:</p> <pre><code>new System.Timers.Timer(1000d / 20); </code></pre> <p>yields a timer that fires 16 times per second, not 20.</p> <p>To be sure that there are no side-effects from too long event handlers, I wrote this little test program:</p> <pre><code>int[] frequencies = { 5, 10, 15, 20, 30, 50, 75, 100, 200, 500 }; // Test System.Timers.Timer foreach (int frequency in frequencies) { int count = 0; // Initialize timer System.Timers.Timer timer = new System.Timers.Timer(1000d / frequency); timer.Elapsed += delegate { Interlocked.Increment(ref count); }; // Count for 10 seconds DateTime start = DateTime.Now; timer.Enabled = true; while (DateTime.Now &lt; start + TimeSpan.FromSeconds(10)) Thread.Sleep(10); timer.Enabled = false; // Calculate actual frequency Console.WriteLine( "Requested frequency: {0}\nActual frequency: {1}\n", frequency, count / 10d); } </code></pre> <p>The output looks like this:</p> <p>Requested: 5 Hz; actual: 4,8 Hz<br> Requested: 10 Hz; actual: 9,1 Hz<br> Requested: 15 Hz; actual: 12,7 Hz<br> Requested: 20 Hz; actual: 16 Hz<br> Requested: 30 Hz; actual: 21,3 Hz<br> Requested: 50 Hz; actual: 31,8 Hz<br> Requested: 75 Hz; actual: 63,9 Hz<br> Requested: 100 Hz; actual: 63,8 Hz<br> Requested: 200 Hz; actual: 63,9 Hz<br> Requested: 500 Hz; actual: 63,9 Hz</p> <p>The actual frequency deviates by up to 36% from the requested one. (And evidently cannot exceed 64 Hz.) Given that Microsoft recommends this timer for its "greater accuracy" over <code>System.Windows.Forms.Timer</code>, this puzzles me.</p> <p>Btw, these are not random deviations. They are the same values every time. And a similar test program for the other timer class, <code>System.Threading.Timer</code>, shows the exact same results.</p> <p>In my actual program, I need to collect measurements at precisely 50 samples per second. This should not yet require a real-time system. And it is very frustrating to get 32 samples per second instead of 50.</p> <p>Any ideas?</p> <p>@Chris: You are right, the intervals all seem to be integer multiples of something around 1/64th second. Btw, adding a Thread.Sleep(...) in the event handler doesn't make any difference. This makes sense given that <code>System.Threading.Timer</code> uses the thread pool, so each event is fired on a free thread.</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.
 

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