Created
June 26, 2011 10:39
-
-
Save jittuu/1047497 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Program | |
| { | |
| [DllImport("Kernel32")] | |
| private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add); | |
| private delegate bool EventHandler(CtrlType sig); | |
| static EventHandler _handler; | |
| enum CtrlType | |
| { | |
| CTRL_C_EVENT = 0, | |
| CTRL_BREAK_EVENT = 1, | |
| CTRL_CLOSE_EVENT = 2, | |
| CTRL_LOGOFF_EVENT = 5, | |
| CTRL_SHUTDOWN_EVENT = 6 | |
| } | |
| private static bool Handler(CtrlType sig) | |
| { | |
| switch (sig) | |
| { | |
| case CtrlType.CTRL_C_EVENT: | |
| case CtrlType.CTRL_LOGOFF_EVENT: | |
| case CtrlType.CTRL_SHUTDOWN_EVENT: | |
| case CtrlType.CTRL_CLOSE_EVENT: | |
| default: | |
| var str = ""; | |
| break; | |
| } | |
| // If it returns true, | |
| // windows prompts "Terminate Process Now" dialog. | |
| return false; | |
| } | |
| static void Main(string[] args) | |
| { | |
| // Some biolerplate to react to close window event | |
| _handler += new EventHandler(Handler); | |
| SetConsoleCtrlHandler(_handler, true); | |
| System.Console.Read(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment