From 169c604c7ab37102140f026bb1d6242856e7ff3d Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Mon, 8 Jun 2026 20:34:25 +0200 Subject: [PATCH] Add process exception handling and clean window closing logic --- Processes/DesktopProcess.cs | 2 +- Processes/TerminalProcess.cs | 7 +++---- Processing/ProcessManager.cs | 22 ++++++++++++++++++++-- UI/GUI/Windows/WindowManager.cs | 11 ++++++++--- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Processes/DesktopProcess.cs b/Processes/DesktopProcess.cs index 051734d..ac31771 100644 --- a/Processes/DesktopProcess.cs +++ b/Processes/DesktopProcess.cs @@ -69,7 +69,7 @@ public class DesktopProcess : Process WindowManager.Update(); // Sleep slightly to yield CPU to the main CLI thread (approx 60 FPS) - //Thread.Sleep(16); + Thread.Sleep(16); } IsRunning = false; diff --git a/Processes/TerminalProcess.cs b/Processes/TerminalProcess.cs index 5ac87ee..cb81df7 100644 --- a/Processes/TerminalProcess.cs +++ b/Processes/TerminalProcess.cs @@ -140,14 +140,13 @@ public class TerminalProcess : Process // The last line is the input line textLines[maxLines].Content = "> " + currentInput + "_"; textLines[maxLines].Position = new Point(5, startY + (maxLines * LineHeight)); - - // Hide any extra text lines we don't need +// Hide any extra text lines we don't need for (int i = maxLines + 1; i < textLines.Count; i++) { textLines[i].Content = ""; } - - window.Flush(); } + + window.Flush(); } } \ No newline at end of file diff --git a/Processing/ProcessManager.cs b/Processing/ProcessManager.cs index 5ba8800..9764989 100644 --- a/Processing/ProcessManager.cs +++ b/Processing/ProcessManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using RemSox.UI.GUI.Windows; namespace RemSox.Processing; @@ -18,7 +19,22 @@ public static class ProcessManager Id = id }; - Thread thread = new(process.Run); + Thread thread = new(() => + { + try + { + process.Run(); + } + catch (Exception ex) + { + Console.WriteLine($"Process {process.Name} (ID: {process.Id}) terminated with an exception: {ex}"); + } + finally + { + processes.TryRemove(id, out _); + WindowManager.CloseWindowsForProcess(id); + } + }); processes.TryAdd(id, (process, thread)); @@ -34,6 +50,8 @@ public static class ProcessManager entry.Process.RequestStop(); + WindowManager.CloseWindowsForProcess(processId); + processes.TryRemove(processId, out _); } @@ -41,7 +59,7 @@ public static class ProcessManager { foreach (var entry in processes.Values) { - entry.Process.RequestStop(); + StopProcess(entry.Process.Id); } processes.Clear(); diff --git a/UI/GUI/Windows/WindowManager.cs b/UI/GUI/Windows/WindowManager.cs index 7b1ef6c..2f6fddc 100644 --- a/UI/GUI/Windows/WindowManager.cs +++ b/UI/GUI/Windows/WindowManager.cs @@ -53,7 +53,7 @@ public static class WindowManager wasLeftButtonDown = leftButtonDown; - if (KeyboardManager.TryReadKey(out KeyEvent keyEvent)) + while (KeyboardManager.TryReadKey(out KeyEvent keyEvent)) { focusedWindow?.HandleKeyEvent(keyEvent); } @@ -99,7 +99,7 @@ public static class WindowManager processWindows.Remove(window); } } - + renderSource.Render(new[] { new RenderCommand { WindowId = window.Id, ElementId = window.Id, ElementType = "WindowClose", Position = window.Position, Properties = new Dictionary() } }); } @@ -117,10 +117,15 @@ public static class WindowManager } public static void CloseWindowsForProcess(Process process) + { + CloseWindowsForProcess(process.Id); + } + + public static void CloseWindowsForProcess(int processId) { lock (windowsLock) { - windows.Remove(process.Id); + windows.Remove(processId); } }