Last active
May 10, 2016 20:26
-
-
Save seesharper/785f5b67e33392d4b363140be84ded90 to your computer and use it in GitHub Desktop.
Kill child process when console terminates
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
| process.Start(); | |
| ProcessKiller.KillWhenConsoleTerminates(process); | |
| RunAndWait(process); |
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
| public static class ProcessKiller | |
| { | |
| [DllImport("Kernel32")] | |
| private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add); | |
| private delegate bool EventHandler(CtrlType sig); | |
| private enum CtrlType { | |
| CTRL_C_EVENT = 0, | |
| CTRL_BREAK_EVENT = 1, | |
| CTRL_CLOSE_EVENT = 2, | |
| CTRL_LOGOFF_EVENT = 5, | |
| CTRL_SHUTDOWN_EVENT = 6 | |
| } | |
| public static void KillWhenConsoleTerminates(Process process) | |
| { | |
| SetConsoleCtrlHandler(ctrlType => {process.Kill(); return false;}, true ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment