title: High CPU usage from while loop, checking for keypress event
body: <p>I have a console application with two threads, one is doing repetitive time consuming work, the other is checking to see if the user has pressed the ESC key. If the ESC key was pressed, the time consuming work thread is paused, an "are you sure" message appears, and if yes is selected, the time consuming work thread finishes its current loop then exits. </p>
  
  <p>The code I have to check for a key-press is using a lot of CPU resources due to the <code>while (!breakCurrentOperation(work)) ;</code> loop. How can I prevent this from happening? </p>
  
  <p>Code: </p>
  
  <pre><code>    public void runTimeConsumingWork()
      {
          HardWork work = new HardWork();
  
          Thread workerThread = new Thread(() =&gt; work.StartWorking());
          workerThread.Start(); // Start the hard work thread
  
          while (!workerThread.IsAlive) ; // Hault untill Thread becomes Active 
  
          // Check if the user wants to stop the hard work
          while (!breakCurrentOperation(work)) ;
  
          // Cancle the hard work
          work.Stop();
  
          // Notify the User
          UserInterfaceController.WriteToConsole("Operation Cancled...");
      }
  
  
      public static bool breakCurrentOperation(HardWork work)
      {
          if (Console.KeyAvailable)
          {
              var consoleKey = Console.ReadKey(true);
              if (consoleKey.Key == ConsoleKey.Escape)
              {
                  work.Pause(); // Pause
                  UserInterfaceController.WriteToConsole("Do you want to stop the current process? \nType s to stop or c to continue.");
                  string input = Console.ReadLine();
                  if (input == "c" || input == "C")
                  {
                      work.Pause(); // Unpause
                      return false; // Continue 
                  }
                  else if (input == "s" || input == "S")
                  {
                      return true; // Break the loop
                  }
                  else
                  {
                      UserInterfaceController.WriteToConsole("Error: Input was not recognized, the current process will now continue. Press Esc to stop the operation.");
                      work.Pause(); // Unpause
                  }
              }
          }
          return false;
      }
  </code></pre>
  
  <p>If I place a <code>Thread.Sleep(2000)</code> in the main console UI thread, CPU usage goes way down, but the application becomes unresponsive with a 2 second delay.  </p>