C # The Win32 console application ignores Ctrl + C and stops the program from exiting,
C # In the Win32 console, the application ignores Ctrl + C and stops the program from exiting. The Windows API SetConsoleCtrlHandler function is used here.
Note: When debugging and executing in VS, set a breakpoint in the processing program routine without interruption. A prompt is displayed, such:
Complete sample code:
Using System; using System. collections. generic; using System. text; using System. diagnostics; using System. runtime. interopServices; using System. threading; /*************************************** * **********************************/* CSharp_Win32 console application ignore Ctrl + C * Powered: testcs_dn * Blog: http://blog.csdn.net/testcs_dn *//************************************ * **********************************/namespace CSharp_Win32 Console the application ignores CtrlC {// defines the handler delegate public delegate bool ConsoleCtrlDelegate (int ctrlType ); class Program {// import SetCtrlHandlerHandler API [DllImport ("kernel32.dll")] private static extern bool SetConsoleCtrlHandler (ConsoleCtrlDelegate HandlerRoutine, bool Add); // when the Console is disabled, the system will send the private const int CTRL_CLOSE_EVENT = 2; // Ctrl + C. The system will send the private const int CTRL_C_EVENT = 0; // Ctrl + break, the system will send the private const int CTRL_BREAK_EVENT = 1; // The user exits (logout), and the system will send the private const int CTRL_LOGOFF_EVENT = 5; // The system is disabled, the system will send the private const int CTRL_SHUTDOWN_EVENT = 6; static void Main (string [] args) {Program cls = new Program (); // Console. readKey ();} public Program () {ConsoleCtrlDelegate consoleDelegete = new handler (HandlerRoutine); bool bRet = SetConsoleCtrlHandler (leledelegete, true); if (bRet = false) // installation event processing failed {Console. writeLine ("error"); while (true) {Console. writeLine ("... "); Thread. sleep (1000) ;}} else {Console. writeLine ("OK"); while (true) {Console. writeLine ("... "); Thread. sleep (1000) ;}}/// <summary> /// process the program routine. Write the code for processing the specified event here. // note: when debugging and executing in VS, set a breakpoint here, but it will not be interrupted; a prompt will be displayed: no available source; /// </summary> /// <param name = "CtrlType"> </param> /// <returns> </returns> private static bool HandlerRoutine (int ctrlType) {switch (ctrlType) {case CTRL_C_EVENT: Console. writeLine ("C"); return true; // return true here, which indicates that the system is blocked from operating the program // break; case CTRL_BREAK_EVENT: Console. writeLine ("BREAK"); break; case CTRL_CLOSE_EVENT: Console. writeLine ("CLOSE"); break; case CTRL_LOGOFF_EVENT: Console. writeLine ("LOGOFF"); break; case CTRL_SHUTDOWN_EVENT: Console. writeLine ("SHUTDOWN"); break;} // return true; // indicates that the response system is blocked from returning false to the program; // ignore the processing, let the system perform default operations }}}