From 67c592a06071b2a179a422177bd14e2913f395b2 Mon Sep 17 00:00:00 2001 From: rafael-aero Date: Wed, 25 Aug 2021 09:38:37 +0200 Subject: [PATCH] kill child process when electronize cli is killed --- ElectronNET.CLI/ProcessHelper.cs | 32 ++++++++++++++++++++++++++++++++ ElectronNET.CLI/Program.cs | 6 ++++++ 2 files changed, 38 insertions(+) diff --git a/ElectronNET.CLI/ProcessHelper.cs b/ElectronNET.CLI/ProcessHelper.cs index 9e0048f..c0290f3 100644 --- a/ElectronNET.CLI/ProcessHelper.cs +++ b/ElectronNET.CLI/ProcessHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Diagnostics; using System.Runtime.InteropServices; @@ -6,6 +7,33 @@ namespace ElectronNET.CLI { public class ProcessHelper { + private static ConcurrentDictionary _activeProcess = new(); + + public static void KillActive() + { + foreach(var kv in _activeProcess) + { + if (!kv.Key.HasExited) + { + try + { + kv.Key.CloseMainWindow(); + } + catch + { + + } + try + { + kv.Key.Kill(false); + } + catch + { + + } + } + } + } public static int CmdExecute(string command, string workingDirectoryPath, bool output = true, bool waitForExit = true) { using (Process cmd = new Process()) @@ -43,7 +71,11 @@ namespace ElectronNET.CLI if (waitForExit) { + _activeProcess[cmd] = true; + cmd.WaitForExit(); + + _activeProcess.TryRemove(cmd, out _); } return cmd.ExitCode; diff --git a/ElectronNET.CLI/Program.cs b/ElectronNET.CLI/Program.cs index b35ef6c..891b15e 100644 --- a/ElectronNET.CLI/Program.cs +++ b/ElectronNET.CLI/Program.cs @@ -19,6 +19,12 @@ namespace ElectronNET.CLI Environment.Exit(-1); } + Console.CancelKeyPress += (s,e) => + { + ProcessHelper.KillActive(); + Environment.Exit(-1); + }; + ICommand command = null; switch (args[0])