title: .net construct for while loop with timeout
body: <p>I commonly employ a while loop that continues to try some operation until either the operation succeeds or a timeout has elapsed:</p>
  
  <pre><code>bool success = false
  int elapsed = 0
  while( ( !success ) &amp;&amp; ( elapsed &lt; 10000 ) )
  {
       Thread.sleep( 1000 );
       elapsed += 1000;
       success = ... some operation ...     
  }
  </code></pre>
  
  <p>I know there a couple of way to implement this, but the basic point is that I repeatedly try some operation with a sleep until success or I've slept too long in aggregate.</p>
  
  <p>Is there a built-in .net class/method/etc to save me from re-writing this pattern all over the place?  Perhaps input is an Func(of bool) and the timeout?
  <br><br><b>Edit</b><br>Thanks to all who contributed. I opted for the sleep() approach because it was the least complicated and I'm totally anti-complexity =)  Here's my (still needs to be tested) implimentation:</p>
  
  <pre><code> public static bool RetryUntilSuccessOrTimeout( Func&lt;bool&gt; task , TimeSpan timeout , TimeSpan pause )
      {
  
          if ( pause.TotalMilliseconds &lt; 0 )
          {
              throw new ArgumentException( "pause must be &gt;= 0 milliseconds" );
          }
          var stopwatch = Stopwatch.StartNew();
          do
          {
              if ( task() ) { return true; }
              Thread.Sleep( ( int )pause.TotalMilliseconds );
          }
          while ( stopwatch.Elapsed &lt; timeout );
          return false;
      }
  </code></pre>