mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-09-22 06:54:44 +00:00
kill child process when electronize cli is killed
This commit is contained in:
@@ -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<Process, bool> _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;
|
||||
|
||||
@@ -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])
|
||||
|
||||
Reference in New Issue
Block a user