title: C# Timer or Thread.Sleep
body: <p>I am running a windows service and using a loop and Thread.Sleep to repeat a task,  would it be better to use a timer method?</p>
  
  <p>If yes a code example would be great</p>
  
  <p>I am currently using this code to repeat</p>
  
  <pre><code>int curMinute;
  int lastMinute = DateTime.Now.AddMinutes(-1).Minute;
  
  while (condition)
  {
     curMinute = DateTime.Now.Minute;
  
     if (lastMinute &lt; curMinute) {
           // do your once-per-minute code here
           lastMinute = curMinute;
     }
  
     Thread.Sleep(50000);      // sleeps for 50 seconds
  
     if (error condition that would break you out of this) {
         break;      // leaves looping structure
     }
  }
  </code></pre>