diff --git a/src/DiffEngineTray/ProcessEx.cs b/src/DiffEngineTray/ProcessEx.cs index 8cf5a7c1..36c52dba 100644 --- a/src/DiffEngineTray/ProcessEx.cs +++ b/src/DiffEngineTray/ProcessEx.cs @@ -18,4 +18,24 @@ public static bool TryGet(int id, [NotNullWhen(true)] out Process? process) return false; } } + + public static void KillAndDispose(this Process process) + { + try + { + process.Kill(); + } + catch (InvalidOperationException) + { + // Race condition can cause "No process is associated with this object" + } + catch (Exception exception) + { + ExceptionHandler.Handle($"Failed to kill process. Id:{process.Id} Name: {process.MainModule?.FileName}", exception); + } + finally + { + process.Dispose(); + } + } } \ No newline at end of file diff --git a/src/DiffEngineTray/Tracker.cs b/src/DiffEngineTray/Tracker.cs index cd06dd6f..bb2ea052 100644 --- a/src/DiffEngineTray/Tracker.cs +++ b/src/DiffEngineTray/Tracker.cs @@ -116,8 +116,6 @@ public TrackedMove AddMove( ProcessEx.TryGet(processId.Value, out process); } - var solution = SolutionDirectoryFinder.Find(key); - Log.Information("MoveAdded. Target:{target}, CanKill:{canKill}, ProcessId:{processId}, CommandLine:{commandLine}", targetFile, canKill, processId, $"{exeFile} {arguments}"); var solution = SolutionDirectoryFinder.Find(key); @@ -220,7 +218,6 @@ static void InnerDiscard(TrackedMove move) FileEx.SafeDelete(move.Temp); } - static void KillProcesses(TrackedMove move) { if (!move.CanKill) @@ -235,27 +232,7 @@ static void KillProcesses(TrackedMove move) return; } - KillProcess(move, move.Process); - } - - static void KillProcess(TrackedMove move, Process process) - { - try - { - process.Kill(); - } - catch (InvalidOperationException) - { - // Race condition can cause "No process is associated with this object" - } - catch (Exception exception) - { - ExceptionHandler.Handle($"Failed to kill process. Command: {move.Exe} {move.Arguments}", exception); - } - finally - { - process.Dispose(); - } + move.Process.KillAndDispose(); } public void Clear() @@ -298,7 +275,7 @@ public void AcceptAll() AcceptAllMoves(); } - public void AcceptAllDeletes() + void AcceptAllDeletes() { foreach (var delete in deletes.Values) {