title: C# Timer resolution: Linux (mono, dotnet core) vs Windows
body: <p>I need a timer that fires every 25ms. I've been comparing the default <code>Timer</code> implementation between Windows 10 and Linux (Ubuntu Server 16.10 and 12.04) on both the dotnet core runtime and the latest mono-runtime.</p>
  
  <p>There are some differences in the timer precision that I don't quite understand.</p>
  
  <p>I'm using the following piece of code to test the Timer:</p>
  
  <pre><code>// inside Main()
          var s = new Stopwatch();
          var offsets = new List&lt;long&gt;();
  
          const int interval = 25;
          using (var t = new Timer((obj) =&gt;
          {
              offsets.Add(s.ElapsedMilliseconds);
              s.Restart();
          }, null, 0, interval))
          {
              s.Start();
              Thread.Sleep(5000);
          }
  
          foreach(var n in offsets)
          {
              Console.WriteLine(n);
          }
  
          Console.WriteLine(offsets.Average(n =&gt; Math.Abs(interval - n)));
  </code></pre>
  
  <p>On windows it's all over the place:</p>
  
  <pre><code>...
  36
  25
  36
  26
  36
  5,8875 # &lt;-- average timing error
  </code></pre>
  
  <p>Using dotnet core on linux, it's less all over the place:</p>
  
  <pre><code>...
  25
  30
  27
  28
  27
  2.59776536312849 # &lt;-- average timing error
  </code></pre>
  
  <p>But the mono <code>Timer</code> is very precise:</p>
  
  <pre><code>...
  25
  25
  24
  25
  25
  25
  0.33 # &lt;-- average timing error
  </code></pre>
  
  <p><strong>Edit:</strong> Even on windows, mono still maintains its timing precision:</p>
  
  <pre><code>...
  25
  25
  25
  25
  25
  25
  25
  24
  0.31
  </code></pre>
  
  <p>What is causing this difference? Is there a benefit to the way the dotnet core runtime does things compared to mono, that justifies the lost precision?</p>