title: How to keep a .NET console app running?
body: <p>Consider a Console application that starts up some services in a separate thread.  All it needs to do is wait for the user to press Ctrl+C to shut it down.  </p>
  
  <p>Which of the following is the better way to do this?</p>
  
  <pre><code>static ManualResetEvent _quitEvent = new ManualResetEvent(false);
  
  static void Main() {
      Console.CancelKeyPress += (sender, eArgs) =&gt; {
          _quitEvent.Set();
          eArgs.Cancel = true;
      };
  
      // kick off asynchronous stuff 
  
      _quitEvent.WaitOne();
  
      // cleanup/shutdown and quit
  }
  </code></pre>
  
  <p>Or this, using Thread.Sleep(1):</p>
  
  <pre><code>static bool _quitFlag = false;
  
  static void Main() {
      Console.CancelKeyPress += delegate {
          _quitFlag = true;
      };
  
      // kick off asynchronous stuff 
  
      while (!_quitFlag) {
          Thread.Sleep(1);
      }
  
      // cleanup/shutdown and quit
  }
  </code></pre>