title: .NET 4.0 System.Timers.Timer seems to not execute the elapsed event after some time
body: <p>I have a class that needs to run 24x7. It uses a Timers.Timer to execute an elapsed event in a calculated interval. After some time, 3-5 days, the timer just stops executing the elapsed event. This does not happen in all of the users, some 4 or 5 reports this. </p>
  
  <pre><code>public class MyClass : IMyClass
  {
      public static MyClass thisMyClass;
  
      private Timer myTimer;
      PERIOD = 60000;
      private SomeHighResolutionClass _lapseTimer;
  
      private double _timeToNextExec
      {
          get
          {
              double lapseTime = _lapseTimer.LapseTime();
              double next = PERIOD - lapseTime;
  
              if (next &gt; 0)
              {
                  return next;
              }
              else
              {
                  return 1;
              }
          }
      }
  
      private MyClass()
      {
          myTimer = new Timer(PERIOD);
          myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
          // Only raise the event the first time Interval elapses.
          myTimer.AutoReset = false;
          myTimer.Enabled = true;
      }
  
      public void myTimer_Elapsed(Object source, ElapsedEventArgs e)
      {
          LogHere("Elapse started"); // Entry log
          try
          {
              try
              {
                  _lapseTimer.Start();
                  DoStuffHere();
              }
              catch (Exception ex)
              {
                  LogException(ex);
              }
          }
          finally
          {
              myTimer.Interval = _timeToNextExec;
              LogHere("restarting timer interval " + myTimer.Interval); // Finally Log
              myTimer.Start();
          }
      }
  
      private void DoStuffHere()
      {
          //some processing that could take 1-2 or more seconds to process
      }
  
      public static void Initialize()
      {
          thisMyClass = new MyClass();    
      }
  
      public static IMyClass Interface
      {
          get { return thisMyClass as IMyClass; }
      }
  }
  </code></pre>
  
  <p>On another class, this is initialized statically.</p>
  
  <pre><code>MyClass.Initialize()
  </code></pre>
  
  <p>The timer interval is variable. and it is based on the elapsed time of the <code>DoStuffHere();</code>. If it takes longer than PERIOD, then it would still be executed as long as DoStuffHere finishes.</p>
  
  <p>Based on the data that we are receiving, the elapsed event is completed and both Entry log and Finally log are logged successfully. 
  We are also sure that the Timer is restarted/enabled based on the memory dump that we received and based on the information from the memory dump, the timer is enabled and should trigger elapsed even but did not. The memory dump was obtained when we already observed that the <code>DoStuffHere()</code> is not getting executed. There are no also no exception as logged in <code>LogExceptions();</code>. </p>
  
  <p><strong>Timer Info From Memory Dump</strong></p>
  
  <pre><code>70dcf7dc  40001e0        4        System.Object  0 instance 00000000 __identity
  703d7618  40002c3        8 ...ponentModel.ISite  0 instance 00000000 site
  703d6b74  40002c4        c ....EventHandlerList  0 instance 00000000 events
  70dcf7dc  40002c2       cc        System.Object  0   static 00000000 EventDisposed
  70dca574  4002dbd       10        System.Double  1 instance 55949.118673 interval
  70dd6820  4002dbe       2c       System.Boolean  1 instance        1 enabled
  70dd6820  4002dbf       2d       System.Boolean  1 instance        0 initializing
  70dd6820  4002dc0       2e       System.Boolean  1 instance        0 delayedEnable
  703ceb80  4002dc1       18 ...apsedEventHandler  0 instance 0246fc8c onIntervalElapsed
  70dd6820  4002dc2       2f       System.Boolean  1 instance        0 autoReset
  703d5fcc  4002dc3       1c ...SynchronizeInvoke  0 instance 00000000 synchronizingObject
  70dd6820  4002dc4       30       System.Boolean  1 instance        0 disposed
  70dcacfc  4002dc5       20 ...m.Threading.Timer  0 instance 0246fcb8 timer
  70dcb07c  4002dc6       24 ...ing.TimerCallback  0 instance 0246fc6c callback
  70dcf7dc  4002dc7       28        System.Object  0 instance 0246fcac cookie
  </code></pre>
  
  <p>I am really puzzled and clueless. Please help guys. </p>
  
  <p>--<strong>UPDATE - Additional information regarding threads</strong> -- The number of threads seems to go up after the Timer has stopped so this may not be the one causing us the problems.</p>
  
  <pre><code>0:000&gt; !threads -live
  ThreadCount:      809
  UnstartedThread:  790
  BackgroundThread: 14
  PendingThread:    790
  DeadThread:       1
  Hosted Runtime:   no
                                     PreEmptive   GC Alloc                Lock
         ID  OSID ThreadOBJ    State GC           Context       Domain   Count APT Exception
     0    1   d4c 012be0a0   2006020 Enabled  00000000:00000000 012b7310     0 STA
     2    2   d54 012c9dd0      b220 Enabled  00000000:00000000 012b7310     0 MTA (Finalizer)
     3    3   d60 0131a0c0   100a220 Enabled  00000000:00000000 012b7310     0 MTA (Threadpool Worker)
     4    4   d64 01331ef0      b220 Enabled  00000000:00000000 012b7310     0 MTA
     6    6   d70 01337348   1000220 Enabled  00000000:00000000 012b7310     0 Ukn (Threadpool Worker)
     7    7   d8c 0133f100   2000220 Enabled  00000000:00000000 012b7310     0 Ukn
     9    a   f90 06c2c948   2000220 Enabled  00000000:00000000 012b7310     0 Ukn
    10    b   9a8 06c33f48   200b020 Enabled  00000000:00000000 012b7310     0 MTA
    11    c   584 06c34450   200b020 Enabled  00000000:00000000 012b7310     0 MTA
    12    d   5f4 06c35758   200b020 Enabled  00000000:00000000 012b7310     0 MTA
    13   12   970 06ca6d08   2000220 Enabled  00000000:00000000 012b7310     0 Ukn
    14    e  1254 06ca8b38   2000220 Enabled  00000000:00000000 012b7310     0 Ukn
    15   13  12f0 06ca9548   2000220 Enabled  00000000:00000000 012b7310     0 Ukn
    16    5  10ec 06ca7210   2000220 Enabled  00000000:00000000 012b7310     0 Ukn
    17    f  1d90 06cac290   a009220 Enabled  259e45ac:259e4fe8 012b7310     0 MTA (Threadpool Completion Port)
    19   19  1124 06ca7718   8009220 Enabled  258f90c4:258fafe8 012b7310     0 MTA (Threadpool Completion Port)
    20    8  1394 06ca9040   2000220 Enabled  00000000:00000000 012b7310     0 Ukn
    22  322  21d4 13698f50   2000220 Enabled  259e6394:259e6fe8 012b7310     0 Ukn
  </code></pre>
  
  <p>Cheers! TIA!</p>