title: Is there a Task based replacement for System.Threading.Timer?
body: <p>I'm new to .Net 4.0's Tasks and I wasn't able to find what I thought would be a Task based replacement or implementation of a Timer, e.g. a periodic Task. Is there such a thing?</p>
  
  <p><em><strong>Update</em></strong>
  I came up with what I think is a solution to my needs which is to wrap the "Timer" functionality inside a Task with child Tasks all taking advantage of the CancellationToken and returns the Task to be able to participate in further Task steps.  </p>
  
  <pre><code>public static Task StartPeriodicTask(Action action, int intervalInMilliseconds, int delayInMilliseconds, CancellationToken cancelToken)
  { 
      Action wrapperAction = () =&gt;
      {
          if (cancelToken.IsCancellationRequested) { return; }
  
          action();
      };
  
      Action mainAction = () =&gt;
      {
          TaskCreationOptions attachedToParent = TaskCreationOptions.AttachedToParent;
  
          if (cancelToken.IsCancellationRequested) { return; }
  
          if (delayInMilliseconds &gt; 0)
              Thread.Sleep(delayInMilliseconds);
  
          while (true)
          {
              if (cancelToken.IsCancellationRequested) { break; }
  
              Task.Factory.StartNew(wrapperAction, cancelToken, attachedToParent, TaskScheduler.Current);
  
              if (cancelToken.IsCancellationRequested || intervalInMilliseconds == Timeout.Infinite) { break; }
  
              Thread.Sleep(intervalInMilliseconds);
          }
      };
  
      return Task.Factory.StartNew(mainAction, cancelToken);
  }      
  </code></pre>