title: Run timer in windows service
body: <p>I have a windows service which is checking in a given path if there are new files created. To do that, I made with a timer which is checking every second the creation of new files in the directory. All the actions of the windows service I'm saving in a log through Log4net library. The problem comes when I start the service. It stops when I want to start the timer.</p>
  
  <p>Thats my window service class:</p>
  
  <pre><code>public partial class WindowsService : ServiceBase
  {
  
      private aTimer timer;
      private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  
      public WindowsService()
      {            
          InitializeComponent();
      }
  
      public void OnDebug()
      {
          OnStart(null);
      }
  
      private void StartTimer()
      {
          timer.StartTimer();
      }
  
      private void StopTimer()
      {
          timer.CloseTimer();
      }
  
  
      protected override void OnStart(string[] args)
      {
          logger.Debug("Service is started.");
          logger.Debug("-----------------------------------------------------------");
          this.StartTimer();
  
  
      }
  
      protected override void OnStop()
      {
          this.StopTimer();         
          logger.Debug("-----------------------------------------------------------");
          logger.Debug("Service is stopped.");
  
      }
  }
  </code></pre>
  
  <p>Thats it's my Timer class:</p>
  
  <pre><code>public class aTimer
  {
      private System.Timers.Timer timer;
      private bool timerTaskSuccess;
  
      private static readonly String path = "C:\\path\\";
      private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  
      public aTimer() { }
  
      public void StartTimer()
      {
          try
          {
              timer = new System.Timers.Timer(1000);
  
              timer.Elapsed += OnTimedEvent;
              timer.AutoReset = true;
              timer.Enabled = true;
              timer.Start();
  
              timerTaskSuccess = false;
          }
          catch (Exception ex)
          {
              logger.ErrorFormat("Error ocurred while starting the timer: '{0}'", ex);
          }
  
          Watcher watcher = new Watcher();
          watcher.CreateWatcher(path);
      }
  
      private void OnTimedEvent(Object source, ElapsedEventArgs e)
      {
          try
          {
              logger.InfoFormat("The Elapsed event was raised at '{0:HH:mm:ss}'", e.SignalTime);
              timerTaskSuccess = true;
          }
          catch (Exception ex)
          {
              logger.ErrorFormat("Error ocurred while catching event the timer: '{0}'", ex);
              timerTaskSuccess = false;
          }
          finally
          {
              if (timerTaskSuccess)
              {
                  timer.Start();
              }
          }
  
      }
  
  
      public void CloseTimer()
      {
          try
          {
              timer.Enabled = false;
              timer.Stop();
              timer.Dispose();
              timer = null;
  
              timerTaskSuccess = true;
          }
          catch (Exception ex)
          {
              logger.ErrorFormat("Error ocurred while stopping the timer: '{0}'", ex);
          }
  
      }
  }
  </code></pre>
  
  <p>Anyone could help me. I test the logical code in a console application and it's working fine. Where is the issue?
  Thanks!</p>