title: C# Wait until condition is true
body: <p>I am trying to write a code that executes when a condition is met. Currently, I am using while...loop, which I know is not very efficient. I am also looking at AutoResetEvent() but i don't know how to implement it such that it keeps checking until the condition is true. </p>
  
  <p>The code also happens to live inside an async method, so may be some kind of await could work? </p>
  
  <pre><code>private async void btnOk_Click(object sender, EventArgs e)
  {
          // Do some work
          Task&lt;string&gt; task = Task.Run(() =&gt; GreatBigMethod());
          string GreatBigMethod = await task;
  
          // Wait until condition is false
          while (!isExcelInteractive())
          {
              Console.WriteLine("Excel is busy");
          }
  
          // Do work
          Console.WriteLine("YAY");
   }
  
  
      private bool isExcelInteractive()
      {
          try
          {
              Globals.ThisWorkbook.Application.Interactive = Globals.ThisWorkbook.Application.Interactive;
              return true; // Excel is free
          }
          catch
          {
              return false; // Excel will throw an exception, meaning its busy
          }
      }
  </code></pre>
  
  <p>I need to find a way to keep checking <code>isExcelInteractive()</code> without CPU stuck in a loop. </p>
  
  <p><strong>Note</strong>: There is no event handler in Excel that would be raised when it is not in edit mode. </p>