title: Timer and exception handling in windows service
body: <p>I had  strange issue with  below mentioned code. I don't understand what is the exact issue. Below code is part of windows service. This code is calling one com class function to perform some operation on message queue after fixed interval.
  I have used System.threading.timer for this purpose. After start, service is working fine for some time but after some time it's  stopped working. </p>
  
  <pre><code>using System;
  using System.IO;
  using System.ServiceProcess;
  using System.Threading;
  using CareMC.VBWrapper.RCWQueue;
  
  namespace MSPQueueService
  {
      public partial class MSPQueueService : ServiceBase
      {
          static System.Threading.Timer timer;
          const int TIMEOUTVALUE = 5000;
          private int isBusy = 0;
          public MSPQueueService()
          {
              InitializeComponent();
          }
  
          protected override void OnStart(string[] args)
          {
              try
              {
                  AppendToLog("ONStart Begin");
                  //Handle Elapsed event
  
                  TimerCallback timerDelegate = new TimerCallback(OnElapsedTime);
  
                  timer = new System.Threading.Timer(timerDelegate, null, TIMEOUTVALUE, TIMEOUTVALUE);
                  //EVENT _objEvent = new EVENT();
                  //_objEvent.ExecuteMSPQueue();
                  AppendToLog("ONStart End");
              }
              catch (Exception ex)
              {
                  string ErrorMessage = ex.Message.ToString();
                  AppendToLog(ErrorMessage);
              }
  
          }
  
          private void OnElapsedTime(object state)
          {
  
              //If the isBusy value is 0 then replace it with 1 and continue otherwise it has already been incremented so we're already running
  
              //the handler so skip out
  
              if (Interlocked.CompareExchange(ref isBusy, 1, 0) != 0)
              {
                  AppendToLog("OnElapsedTime is already running. isBusy=" + isBusy.ToString());
                  return;
              }
  
              try
              {
  
                  EVENT _objEvent = new EVENT();
                  AppendToLog("Before Executing ExecuteMSPQueue function.");
                  _objEvent.ExecuteMSPQueue();
                  AppendToLog("After Executing ExecuteMSPQueue function.");
              }
              catch (Exception ex)
              {
                  AppendToLog(ex.ToString());
              }
              finally
              {
                  AppendToLog("Resetting  isBusy value to 0=");
                  Interlocked.Exchange(ref isBusy, 0);
                  AppendToLog("Value of   isBusy  after reset =" + isBusy);
  
              };
  
          }
  
  
  }
  </code></pre>
  
  <p>Here is smaple log from the code</p>
  
  <blockquote>
    <p>10/11/2011 4:15:03 AM: Before Executing ExecuteMSPQueue function.</p>
    
    <p>10/11/2011 4:15:08 AM: OnElapsedTime is already running.</p>
    
    <p>10/11/2011 4:15:13 AM: OnElapsedTime is already running.</p>
    
    <p>10/11/2011 4:15:18 AM: OnElapsedTime is already running.</p>
    
    <p>10/11/2011 4:15:19 AM: After Executing ExecuteMSPQueue function.</p>
    
    <p>10/11/2011 4:15:28 AM: OnElapsedTime is already running.</p>
    
    <p>10/11/2011 4:15:33 AM: OnElapsedTime is already running.</p>
    
    <p>10/11/2011 4:15:38 AM: OnElapsedTime is already running.</p>
    
    <p>10/11/2011 4:15:43 AM: OnElapsedTime is already running.</p>
    
    <p>10/11/2011 4:15:48 AM: OnElapsedTime is already running.</p>
    
    <p>10/11/2011 4:15:53 AM: OnElapsedTime is already running.</p>
  </blockquote>
  
  <p>This means value of IsBusy variable remains 1 and not reseting to 0 . by looking at  code it is only possible if there is exception which not gets caught in catch block.(is this possible?)</p>
  
  <p>Please help me.</p>