How do I use Console.CancelKeyPress in .NET 4? (Works fine in .NET 3.5 and below)
问题 I am writing a Console app in C# 4 and want to gracefully cancel my program and Ctrl + C is pressed. The following code I have used many times before, but now when trying to use it in .NET 4, it seems a strange unhandled exception is occurring. namespace ConsoleTest { class Program { private static bool stop = false; static void Main(string[] args) { System.Console.TreatControlCAsInput = false; System.Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress); while (