Add process exception handling and clean window closing logic

This commit is contained in:
Stone_Red
2026-06-08 20:34:25 +02:00
parent 82efb8d967
commit 169c604c7a
4 changed files with 32 additions and 10 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ public class DesktopProcess : Process
WindowManager.Update(); WindowManager.Update();
// Sleep slightly to yield CPU to the main CLI thread (approx 60 FPS) // Sleep slightly to yield CPU to the main CLI thread (approx 60 FPS)
//Thread.Sleep(16); Thread.Sleep(16);
} }
IsRunning = false; IsRunning = false;
+3 -4
View File
@@ -140,14 +140,13 @@ public class TerminalProcess : Process
// The last line is the input line // The last line is the input line
textLines[maxLines].Content = "> " + currentInput + "_"; textLines[maxLines].Content = "> " + currentInput + "_";
textLines[maxLines].Position = new Point(5, startY + (maxLines * LineHeight)); 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++) for (int i = maxLines + 1; i < textLines.Count; i++)
{ {
textLines[i].Content = ""; textLines[i].Content = "";
} }
window.Flush();
} }
window.Flush();
} }
} }
+20 -2
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using RemSox.UI.GUI.Windows;
namespace RemSox.Processing; namespace RemSox.Processing;
@@ -18,7 +19,22 @@ public static class ProcessManager
Id = id 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)); processes.TryAdd(id, (process, thread));
@@ -34,6 +50,8 @@ public static class ProcessManager
entry.Process.RequestStop(); entry.Process.RequestStop();
WindowManager.CloseWindowsForProcess(processId);
processes.TryRemove(processId, out _); processes.TryRemove(processId, out _);
} }
@@ -41,7 +59,7 @@ public static class ProcessManager
{ {
foreach (var entry in processes.Values) foreach (var entry in processes.Values)
{ {
entry.Process.RequestStop(); StopProcess(entry.Process.Id);
} }
processes.Clear(); processes.Clear();
+8 -3
View File
@@ -53,7 +53,7 @@ public static class WindowManager
wasLeftButtonDown = leftButtonDown; wasLeftButtonDown = leftButtonDown;
if (KeyboardManager.TryReadKey(out KeyEvent keyEvent)) while (KeyboardManager.TryReadKey(out KeyEvent keyEvent))
{ {
focusedWindow?.HandleKeyEvent(keyEvent); focusedWindow?.HandleKeyEvent(keyEvent);
} }
@@ -99,7 +99,7 @@ public static class WindowManager
processWindows.Remove(window); processWindows.Remove(window);
} }
} }
renderSource.Render(new[] { new RenderCommand { WindowId = window.Id, ElementId = window.Id, ElementType = "WindowClose", Position = window.Position, Properties = new Dictionary<string, object?>() } }); renderSource.Render(new[] { new RenderCommand { WindowId = window.Id, ElementId = window.Id, ElementType = "WindowClose", Position = window.Position, Properties = new Dictionary<string, object?>() } });
} }
@@ -117,10 +117,15 @@ public static class WindowManager
} }
public static void CloseWindowsForProcess(Process process) public static void CloseWindowsForProcess(Process process)
{
CloseWindowsForProcess(process.Id);
}
public static void CloseWindowsForProcess(int processId)
{ {
lock (windowsLock) lock (windowsLock)
{ {
windows.Remove(process.Id); windows.Remove(processId);
} }
} }