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:
?
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Runtime.InteropServices;using System.Threading; /************************************************************************//* The CSharp_Win32 console application ignores Ctrl + C * Powered by:testcs_dn * Blog:http://blog.csdn.net/testcs_dn *//************************************************************************/The namespace CSharp_Win32 console application ignores CtrlC{ // Define 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 user closes the Console, the system sends a message. private const int CTRL_CLOSE_EVENT = 2; // Ctrl + C, the system will send the message private const int CTRL_C_EVENT = 0; // Ctrl + break, the system will send the message private const int CTRL_BREAK_EVENT = 1; // Log out (logout) and the system sends the message private const int CTRL_LOGOFF_EVENT = 5; // When the system is disabled, the system sends a message. private const int CTRL_SHUTDOWN_EVENT = 6; static void Main(string[] args) { Program cls = new Program(); //Console.ReadKey(); } public Program() { ConsoleCtrlDelegate consoleDelegete = new ConsoleCtrlDelegate(HandlerRoutine); bool bRet = SetConsoleCtrlHandler(consoleDelegete, true); if (bRet == false) // An error occurred while handling the installation event. { Console.WriteLine("error"); while (true) { Console.WriteLine("..."); Thread.Sleep(1000); } } else { Console.WriteLine("ok"); while (true) { Console.WriteLine("..."); Thread.Sleep(1000); } } } /// <summary> /// Processing program routine, where you can compile the processing program code for the specified event /// 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"> /// <returns></returns> private static bool HandlerRoutine(int ctrlType) { switch(ctrlType) { case CTRL_C_EVENT: Console.WriteLine("C"); return true; // Returns true, which indicates that the system is blocked from performing operations on 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 system is blocked from performing operations on the program. return false;// Ignore and allow the system to perform default operations } }} |