title: Single threaded timer and/or interval inside a Task<T>
body: <p>I want to run a function on an interval, inside of a Task. Something like, </p>
  
  <pre><code>       Task t = Task.Factory.StartNew(() =&gt; {
              while (notCanceled()) {
                  doSomething();
                  Thread.Sleep(interval); 
              }
          }); 
  </code></pre>
  
  <p>Is it a bad idea to use Thread.Sleep() here? The task is long running, and the sleep time may also be very long (minutes, maybe even hours). </p>
  
  <p>One alternative is to use either System.Timers.Timer or System.Threading.Timer. However both of these would cause an additional thread to spawn (the Elapsed events occur on a new threadpool thread). So for every repeating task, there would be 2 threads instead of 1. The Task is already asynchronous so I'd prefer not to complicate things in this way. </p>
  
  <p>Yet another way that behaves similarly is to use ManualResetEvent, </p>
  
  <pre><code>    ManualResetEvent m = new ManualResetEvent(false);
  
      void sleep(int milliseconds)
      {
          m.WaitOne(milliseconds);
      } 
  </code></pre>
  
  <p>Since m.Set() would never be called, this would always wait for the right amount of time, and also be single threaded. Does this have any significant advantage over Thread.Sleep()?</p>
  
  <p>Wondering what the best practice would be here. </p>
  
  <p>Thoughts? </p>