This time, I'll present a runnable program. The ConsoleCtrl class is designed to intercept CTRL-C and, for testing purposes, will exit after it gets 10 of them. Or after a little over 27 hours.

What's wrong with this code?

using System;
using System.Threading;
using System.Runtime.InteropServices;

public class ConsoleCtrl
{

 public enum ConsoleEvent
 {
  CTRL_C = 0,  // From wincom.h
  CTRL_BREAK = 1,
  CTRL_CLOSE = 2,
  CTRL_LOGOFF = 5,
  CTRL_SHUTDOWN = 6
 }

 public delegate void ControlEventHandler(ConsoleEvent consoleEvent);
 public event ControlEventHandler ControlEvent;

 public ConsoleCtrl()
 {
  SetConsoleCtrlHandler(new ControlEventHandler(Handler), true);
 }

 private void Handler(ConsoleEvent consoleEvent)
 {
  if (ControlEvent != null)
   ControlEvent(consoleEvent);
 }

 [DllImport("kernel32.dll")]
 static extern bool SetConsoleCtrlHandler(ControlEventHandler e, bool add);
}


public class Program
{
 static int count = 0;

 public static void Main()
 {
  ConsoleCtrl consoleCtrl = new ConsoleCtrl();

  consoleCtrl.ControlEvent += new ConsoleCtrl.ControlEventHandler(Logger);

  Thread.Sleep(100000);
 }

 private static void Logger(ConsoleCtrl.ConsoleEvent consoleEvent)
 {
  Console.WriteLine(consoleEvent);
  count++;

  if (count == 10)
  {
   Environment.Exit(0);
  }
 }

}