title: How to use a Timer to replace Thread.Sleep(...) in an Azure Worker Role?
body: <p>There's plenty of examples of people saying to use a <code>Timer</code> instead of <code>Thread.Sleep(...)</code> in an <code>Azure Worker Role</code>. No probs with that.</p>
  <p>What I'm struggling to understand is how to code this.</p>
  <p>Currently, I have the following (pseduo code)</p>
  <pre><code>_timer.Elapsed += (sender, args) =&gt; DoWork();
  
  public override void Run()
  {
      while(true)
      {
          DoWork();
      }
  }
  
  public void DoWork()
  {
      try
      {
          _timer.Stop();
  
          // Now - do stuff ....
  
       }
       catch(....) { ... }
  
       _timer.Start()
  }
  </code></pre>
  <p>And what happens, is that the code enters the <code>DoWork()</code> method once and <code>DoesStuff(tm)</code>.. fine .. starts the timer (say .. with a 30 second interval) and then exits that method.</p>
  <p>Then, it returns back to the main <code>Run()</code> method .. which is in that loop. So it <strong>immediately</strong> comes back around and enters the <code>DoWork()</code> method again .. instead of waiting for the timer to fire it off.</p>
  <p>So I'm not sure how to replace any <code>Thread.Sleep(...)</code> with Timers.</p>
  <p>Any clues?</p>
  <h3>Clarification</h3>
  <p>I do not want to exit the <code>Run()</code> method :) I'm very happy to keep looping forever. What I'm stuck with, is replacing the standard <code>Thread.Sleep(...)</code> call (which blocks the thread) and replace that with a <code>Timer</code>, which most people suggest.</p>
  <h3>Update</h3>
  <p>Please do not link or suggest that I should use <code>cancelSource.Token.WaitHandle.WaitOne();</code> as a solution. That is not what I'm trying to achieve here. Please note the post title!</p>