C # The Win32 console application ignores Ctrl + C and stops the program from exiting,

Source: Internet
Author: User

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 }}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.