fix(Program.cs): add try-catch block to handle potential errors when getting the foreground process

This commit is contained in:
2025-08-26 21:50:58 +02:00
parent 458a466c15
commit bad2313c18

View File

@@ -246,11 +246,15 @@ namespace DD2Switcher {
} }
private static void TabToPrevious() { private static void TabToPrevious() {
try {
var foreground = GetForegroundProcess(); var foreground = GetForegroundProcess();
if (!ProcessTracked(foreground.Id)) { if (!ProcessTracked(foreground.Id)) {
Console.WriteLine("Foreground process not tracked, skipping"); Console.WriteLine("Foreground process not tracked, skipping");
return; return;
} }
} catch (Exception e) {
Console.WriteLine($"Error setting foreground window: {e}");
}
if (lastWindows.Count == 0) { if (lastWindows.Count == 0) {
Console.WriteLine("No previous window to switch to"); Console.WriteLine("No previous window to switch to");
@@ -261,7 +265,7 @@ namespace DD2Switcher {
for (int i = lastWindows.Count - 1; i >= 0; i--) { for (int i = lastWindows.Count - 1; i >= 0; i--) {
int index = lastWindows[i]; int index = lastWindows[i];
if (index != ActiveIndex) { if (index != ActiveIndex) {
TabTo(index+1); // Our windows are 1-indexed because... I don't remember why TabTo(index + 1); // Our windows are 1-indexed because... I don't remember why
break; break;
} }
} }