title: System.Timers.Timer massively inaccurate
body: <p>I've written a program which uses all available cores by using <code>Parallel.ForEach</code>. The list for the <code>ForEach</code> contains ~1000 objects and the computation for each object take some time (~10 sec).
  In this scenario I setup a timer like this:</p>
  
  <pre><code>timer = new System.Timers.Timer();
  timer.Elapsed += TimerHandler;
  timer.Interval = 15000;
  timer.Enabled = true;
  
  private void TimerHandler(object source, ElapsedEventArgs e)
  {
          Console.WriteLine(DateTime.Now + ": Timer fired");
  }
  </code></pre>
  
  <p>At the moment the <code>TimerHandler</code> method is a stub to make sure the problem isn't caused by this method.</p>
  
  <p>My expectation was that the <code>TimerHandler</code> method will be executed every ~15 seconds. However, the time between two calls to this method even reaches 40 seconds, so 25 seconds too much.
  By using <code>new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount -1 }</code> for the <code>Parallel.ForEach</code> method this doesn't happen and the expected interval of 15 seconds is seen.</p>
  
  <p>Is it intended that I have to make sure that there is always one core available per active timer? Seems to be a bit odd even more, because the "reserved" could be a valuable resource for my computation.</p>
  
  <p>Edit: As indicated by Yuval setting a fixed minimum of threads in the pool by <code>ThreadPool.SetMinThreads</code> solved the problem. I also tried <code>new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }</code> (so without the <code>-1</code> in the initial question) for the <code>Parallel.ForEach</code> method and this also solves the problem. However, I have no good explanation why these modifications solved the problem. Maybe there were so many threads created that the timer thread just got "lost" for a "long" time until it was executed again.</p>