When I run the below in Release, pressing CTRL + C successfully terminates the application. 
When running in Debug, pressing CTRL + C hangs in the while loop below. 
Why? Is there a way around this?
static void Main(string[] args)
{
    while (true)
    {
        // Press CTRL + C...
        // When running in Release, the app closes down
        // When running in Debug, it hangs in here
    }
}
                One way is to achieve this is to use Console.CancelKeyPress
Occurs when the Control modifier key
(Ctrl)and either theConsoleKey.Cconsole key (C) or the Break key are pressed simultaneously(Ctrl+C or Ctrl+Break).When the user presses either
Ctrl+CorCtrl+Break, theCancelKeyPressevent is fired and the application'sConsoleCancelEventHandlerevent handler is executed. The event handler is passed aConsoleCancelEventArgsobject
Example
private static bool keepRunning = true;
public static void Main(string[] args)
{
   Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) {
         e.Cancel = true;
         keepRunning = false;
      };
   while (keepRunning) 
   {
      // Do stuff
   }
   Console.WriteLine("exited gracefully");
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With