title: C# : Wait for n miliseconds, and test a boolean every n/m milliseconds
body: <p>I have to test a boolean every 250ms, max 2 seconds, and stop when it's true.<br>
  Tried this, but Thread.Sleep isn't a good solution for me because it's freezing my software, and waiting for even more won't change anything because the boolean won't change while it's frozen.   </p>
  
  <pre><code>bool onLine = false;
  Stopwatch chrono = new Stopwatch();
  chrono.Start();
  while (chrono.ElapsedMilliseconds &lt; 2000)
  {
      if (!State().Equals(Online))
      {
          System.Threading.Thread.Sleep(250);
      }
      else
      {
          onLine = true;
          operationCompleted();
          break;
      }
  }
  if (!onLine)
  {
      MessageBox.Show("Error");
  }
  </code></pre>
  
  <p>I don't know how to make the tests in an event raised by a timer if someone has a correct implementation? </p>