title: Best possible way to prevent Elapsed event of timer in windows service from firing till one cycle has completed
body: <p>I have a windows service which has a timer reading interval from configuration.</p>
  
  <p>DipRedipServiceTimer_Elapsed event is called after 1000 miliseconds on first run of service. Code written in this method must be executed before running next cycle of this timer. however, i have seen DipRedipServiceTimer_Elapsed event firing even when the first cycle has not yet completed. this results is two threads working on same piece of code and some very horrible issues. How can i prevent this from happening ? Please suggest.</p>
  
  <pre><code>partial class DIPREDIPServiceHost : ServiceBase
  {
      #region Private Fields
      /// &lt;summary&gt;
      /// Timer for polling job pool on timely basis 
      /// &lt;/summary&gt;
      private Timer DipRedipServiceTimer;                                   
      #endregion
      public DIPREDIPServiceHost()
      {
          InitializeComponent();
      }
      protected override void OnStart(string[] args)
      {                    
          DipRedipServiceTimer = new Timer();
          DipRedipServiceTimer.Enabled = true;            
          DipRedipServiceTimer.Interval = 1000;
          DipRedipServiceTimer.AutoReset = false; 
          DipRedipServiceTimer.Elapsed += new ElapsedEventHandler(DipRedipServiceTimer_Elapsed);                        
      }
      protected override void OnStop()
      {
          if (DipRedipServiceTimer != null)
          {
              DipRedipServiceTimer.Enabled = false;
              DipRedipServiceTimer.Stop();
              DipRedipServiceTimer = null;
          }
      }
      #region "Timer Elapsed Event"
      /// &lt;summary&gt;
      /// Handles the Elapsed event of the timer control.
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;The source of the event.&lt;/param&gt;
      /// &lt;param name="e"&gt;The &lt;see cref="System.Timers.ElapsedEventArgs"/&gt; instance containing the event data.&lt;/param&gt;
      void DipRedipServiceTimer_Elapsed(object sender, ElapsedEventArgs e)
      {
          //disable timers as at a given time only one thread should process dip/redip. 
          DipRedipServiceTimer.Stop();
          DipRedipServiceTimer.AutoReset = false;
          DipRedipServiceTimer.Enabled = false;
          try
          {
              IDipRedipController controller = new DipRedipController();
              try
              {
                  DipRedipConfiguration config = controller.GetDipRedipConfiguration();
                  // In case configuration has been retrieved, set timer defined.
                  if (config != null)
                  {
                      //set timer interval after reading from config file.
                      DipRedipServiceTimer.Interval = config.FileGenerationInterval * 60000;
                      controller.dipRedipConfiguration = config;
                      LoggingHelper.LogMessage(String.Format("Dip Service timer initialized at {0}", DateTime.UtcNow), Source.EDiscDIPREDIPService, LogCategory.Exception);
                      //Process Dip
                      bool dipSuccess = controller.ProcessDIP();
                      //Process Re-Dip
                      bool redipSuccess = controller.ProcessREDIP();
                      //Enable timers for next cycle
                      LoggingHelper.LogMessage(String.Format("Dip Service timer completed at {0}", DateTime.UtcNow), Source.EDiscDIPREDIPService, LogCategory.Exception);
                  }
                  // In case configuration is null, get the default timer defined in App.Config file.
                  else
                  {
                      int interval = 0;
                      int.TryParse(ConfigurationManager.AppSettings.Get("DefaultTimerValue"), out interval);
                      DipRedipServiceTimer.Interval = interval * 60000;
                      LoggingHelper.LogWarning("Configuration for Dip/Redip could not be fetched from database.", Source.FileImportService, LogCategory.Exception);
                  }                    
                  DipRedipServiceTimer.Enabled = true;
                  DipRedipServiceTimer.Start(); 
              }
              catch (FaultException ex)
              {
                  LoggingHelper.LogException("Exception Occured in DipRedipServiceTimer_Elapsed method of Dip/Redip Window Service", ex, Source.EDiscDIPREDIPService);                    
              }
          }
          catch (Exception ex)
          {
              LoggingHelper.LogException("Exception Occured in the DipRedip Service Host Window Service", ex, Source.EDiscDIPREDIPService);               
          }
       }
      #endregion                 
  }
  </code></pre>