title: How to prevent System.Timers.Timer from queuing for execution on a thread pool?
body: <p>There is a problem with standard System.Timers.Timer behaviour. The timer raise Elapsed event with some interval. But when time of execution inside Elapsed event handler exceed timer interval then thread pool begin queuing event handling. This is a problem in my case. This is because with my Elapsed event handler I fetch some data from database and doing something with it and finally save results back to database. But data handling should be provided only once. So, is there a way to prevent from queuing elapse events for System.Timers.Timer.</p>
  
  <p>As illustration for this issue you can consider next test program:</p>
  
  <pre><code>public class EntryPoint
  {
  
      private static void TimeProc(object state, ElapsedEventArgs e)
      {
          Console.WriteLine("Current time {0} on the thread {1}", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
          Thread.Sleep(20000);
      }
  
      static void Main(string[] args)
      {
          Console.WriteLine("Press &lt;Enter&gt; for finishing\n\n");
          ThreadPool.SetMaxThreads(10, 10);
          System.Timers.Timer MyTimer = new System.Timers.Timer(1000);
          MyTimer.Elapsed += new ElapsedEventHandler(TimeProc);
          MyTimer.Start();
          Console.ReadLine();
          MyTimer.Stop();
      }
  }
  </code></pre>
  
  <p>And possible output will be as here:</p>
  
  <pre><code>Current time 03.02.2011 0:00:09 on the thread 4
  Current time 03.02.2011 0:00:10 on the thread 5
  Current time 03.02.2011 0:00:12 on the thread 6
  Current time 03.02.2011 0:00:13 on the thread 7
  Current time 03.02.2011 0:00:14 on the thread 8
  Current time 03.02.2011 0:00:15 on the thread 9
  Current time 03.02.2011 0:00:16 on the thread 10
  Current time 03.02.2011 0:00:17 on the thread 11
  Current time 03.02.2011 0:00:18 on the thread 12
  Current time 03.02.2011 0:00:19 on the thread 13
  Current time 03.02.2011 0:00:30 on the thread 4
  Current time 03.02.2011 0:00:30 on the thread 5
  </code></pre>
  
  <p><strong>Possible solutions:</strong></p>
  
  <p>1) It  was inspired by:<a href="https://stackoverflow.com/questions/3474694/c-timer-vs-thread-in-service">C# Timer vs Thread in Service</a> </p>
  
  <p>And has a code like here regarding to mentioned above sample:</p>
  
  <pre><code>    public class EntryPoint
      {
          private static System.Timers.Timer MyTimer;
          private static void TimeProc(object state, ElapsedEventArgs e)
          {
              Console.WriteLine("Current time {0} on the thread {1}", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
              Thread.Sleep(20000);
              MyTimer.Enabled = true;
          }
  
          static void Main(string[] args)
          {
              Console.WriteLine("Press &lt;Enter&gt; for finishing\n\n");
              ThreadPool.SetMaxThreads(10, 10);
              MyTimer = new System.Timers.Timer(1000);
              MyTimer.AutoReset = false;
  
              MyTimer.Elapsed += new ElapsedEventHandler(TimeProc);
              MyTimer.Enabled = true;
              Console.ReadLine();
  
          }
      }
  </code></pre>
  
  <p>2) Second way is about SynchronizingObject, but it is a valuable only for Windows form application or required additional development of code for implementing object that would be implements ISynchronizeInvoke interface. More about this way you can find <a href="https://stackoverflow.com/questions/1841345/synchronizingobject-for-an-event">here</a></p>
  
  <p>So, for now I will prefer first solution.</p>