title: Multimedia Timer limitations or why it stops firing randomly
body: <p>I have a program that responds to an event X. I need to react to this event within ms and do a job a few times. This can take up to 30''. Therefore, I created a class which I ping once I receive the event and I launch a multimedia timer.
  This can be called several times but usually up to 6 timers at the same time at most. My application starts great, NEVER throws warnings/exceptions, I can't see memory leaks... But randomly, <strong>if my application has processed quite a few events, it stops firing the multimedia timers. I can see how it is called but it never calls the callback function anymore. No exceptions, nothing. Why is this happenning?</strong> I have been stuck here for two weeks.</p>
  
  <p>Here is my code, this is the snipped from the class A that is fired when event X happens:</p>
  
  <pre><code>private TimerEventHandler timerClickingWLRef;
  private uint timerIdSeating = 0;
  ...
          private void launchMultiMedia()
      {
          try
          {
              timerRef = new TimerEventHandler(CheckMultiMedia);
              timerId = timeSetEvent(10, 5, timerRef, UIntPtr.Zero, TIME_KILL_SYNCHRONOUS | TIME_PERIODIC);
          }
          catch (Exception ex)
          {
              Console.WriteLine("Exception: " + ex.InnerException.ToString());
          }
          stopwatch.Start();          
      }
  
  
  private void CheckMultiMedia(uint id, uint msg, UIntPtr userCtx, UIntPtr uIntPtr, UIntPtr intPtr)
          {
  //This part of the code is no longer reached after a bit
              if (IsWindow(h))
              {
              try
              {
                  // Must be initialize to false in order to work
                  acquiredLock = false;
  
                  System.Threading.Monitor.TryEnter(syncThreadsC, ref acquiredLock);
                  if (acquiredLock &amp;&amp; continue)
                  {
                      if (boolvar)
                      {
                          if (stopwatch.ElapsedMilliseconds &lt; 31000) 
                          {
                              // Do sth very fast
                          }
                          else
                          {
                              StopMultiMedia();
                          }
  
                      }
                  }
              }
              finally
              {
                  if (acquiredLockCheck)
                      System.Threading.Monitor.Exit(syncThreadsC);
              }
          }
          else
          {
              StopMultiMedia();
          }
      }
  </code></pre>
  
  <p>In order to avoid a <code>NullReferenceException</code> when deleting the object, I have implemented a dispose pattern:</p>
  
  <pre><code>public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
      protected virtual void Dispose(bool disposing)
      {
          if (disposed)
              return;
  
          // Free any unmanaged objects here.
          if (timerId != 0)
          {
              timeKillEvent(timerId);
              timerId = 0;
          }
  
          disposed = true;
  
          if (disposing)
          {
              // Free any other managed objects here.
          }
      }
  
      ~ClassA()
      {
          Dispose(false);
      }
  </code></pre>
  
  <p>I don't understand why it starts working well and then, randomly (it seems after application been a bit busy firing these events, but never more than 5-6 at the same time) it stops firing. I can see <code>LaunchMultimedia</code>() is called and <code>StopWatch.Start()</code> starts working, however, <code>the callback function</code> is not called anymore.</p>
  
  <p>Is there any kind of limitation or similar that prevents the multimedia timer to be fired again at all?</p>
  
  <p>I don't know where to look with no exceptions, warning or errors in the code.</p>
  
  <p>With the information I have I can only assume code is not that important but some condition affecting on it.</p>
  
  <p>If you need more code just let me know.</p>