title: How can I get my windows service to run at a specific interval?
body: <p>I want to write a windows service. But it needs to run at specific times. For example, my service should email me every 5 minutes.</p>
  
  <pre><code>private Timer _timer;
  private DateTime _lastRun = DateTime.Now;
  
  protected override void OnStart(string[] args)
  {
      _timer = new Timer(10 * 60 * 1000); // every 10 minutes
      _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
      //...
  }
  
  
  private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
      // ignore the time, just compare the date
      if (_lastRun.Date &lt; DateTime.Now.Date)
      {
          // stop the timer while we are running the cleanup task
          _timer.Stop();
          //
          // do cleanup stuff
          //
          _lastRun = DateTime.Now;
          _timer.Start();
      }
  }
  </code></pre>
  
  <p>I want my service to email me every 5 minutes.</p>