Handling CTRL-C in .NET Console Applications

By default, pressing CTRL-C while a console application is running will cause it to terminate.

If we want to prevent this we can set Console.TreatControlCAsInput Property to true. This will prevent CTRL-C from terminating the application. To terminate now, the user needs to close the console window or hit CTRL-BREAK instead of the more usual and well-known CTRL-C.

class Program
{
    private static void Main(string[] args)
    {
        Console.TreatControlCAsInput = true;
        while (true)
        {
        }
    }
}

There is also the Console.CancelKeyPress event. This event is fired whenever CTRL-C or CTRL-BREAK is pressed. It allows us to decide whether or not to terminate the application.

The following code will always prevent termination from both CTRL-C and CTRL-BREAK. Termination is prevented by setting the  ConsoleCancelEventArgs.Cancel property to true:

class Program
{
    private static void Main(string[] args)
    {
        Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) =>
                                  {
                                      e.Cancel = true;
                                  };
        while (true)
        {
        }
    }        
}

Note: “In the .NET Framework 3.5 and .NET Framework 4, attempting to set the Cancel property to true if the CancelKeyPress event was invoked by the user pressing Ctrl+Break threw an InvalidOperationException exception. In the .NET Framework 4.5, you can set the Cancel property to true after the user presses Ctrl+Break and cancel the termination of the application.” -  MSDN.

We can also use the ConsoleCancelEventArgs.SpecialKey property to check which key combination triggered the event. In the last example we prevent termination on CTRL-C but still allow termination with CTRL-BREAK.

class Program
{
    private static void Main(string[] args)
    {
        Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) =>
                                  {

                                      var isCtrlC = e.SpecialKey == ConsoleSpecialKey.ControlC;
                                      var isCtrlBreak = e.SpecialKey == ConsoleSpecialKey.ControlBreak;

                                      // Prevent CTRL-C from terminating
                                      if (isCtrlC)
                                      {
                                          e.Cancel = true;    
                                      }

                                      // e.Cancel defaults to false so CTRL-BREAK will still cause termination
                                      
                                  };
        while (true)
        {
        }
    }        
}

For more console related tips, check out my Building .NET Console Applications in C# Pluralsight course.

You can start watching with a Pluralsight free trial.

SHARE:

Comments (2) -

  • qbert

    4/7/2020 12:46:38 PM | Reply

    thanks for the 13  liner in the middle , it was helpful to cpature ctrlc

    • Jason Roberts

      4/8/2020 1:44:20 AM | Reply

      Thanks - glad it was of use! Smile

Add comment

Loading