From 6e95046af297164b0c3ba18ce36c33a2315ddd52 Mon Sep 17 00:00:00 2001 From: bluepilledgreat <97983689+bluepilledgreat@users.noreply.github.com> Date: Sat, 4 May 2024 12:40:43 +0100 Subject: [PATCH 1/2] safer getprocesses thanks microsoft --- Bloxstrap/App.xaml.cs | 2 +- Bloxstrap/Bootstrapper.cs | 2 +- Bloxstrap/Utilities.cs | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index 4118020..3ec400d 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -180,7 +180,7 @@ namespace Bloxstrap if (LaunchSettings.IsMenuLaunch) { - Process? menuProcess = Process.GetProcesses().Where(x => x.MainWindowTitle == $"{ProjectName} Menu").FirstOrDefault(); + Process? menuProcess = Utilities.GetProcessesSafe().Where(x => x.MainWindowTitle == $"{ProjectName} Menu").FirstOrDefault(); if (menuProcess is not null) { diff --git a/Bloxstrap/Bootstrapper.cs b/Bloxstrap/Bootstrapper.cs index 909c978..ff34f4b 100644 --- a/Bloxstrap/Bootstrapper.cs +++ b/Bloxstrap/Bootstrapper.cs @@ -415,7 +415,7 @@ namespace Bloxstrap App.Logger.WriteLine(LOG_IDENT, "Waiting for Roblox to close"); - while (Process.GetProcesses().Any(x => x.Id == gameClientPid)) + while (Utilities.GetProcessesSafe().Any(x => x.Id == gameClientPid)) await Task.Delay(1000); App.Logger.WriteLine(LOG_IDENT, $"Roblox has exited"); diff --git a/Bloxstrap/Utilities.cs b/Bloxstrap/Utilities.cs index eaf3816..7bacb94 100644 --- a/Bloxstrap/Utilities.cs +++ b/Bloxstrap/Utilities.cs @@ -74,5 +74,20 @@ namespace Bloxstrap return versionInfo.ProductVersion.Replace(", ", "."); } + + public static Process[] GetProcessesSafe() + { + const string LOG_IDENT = "Utilities::GetProcessesSafe"; + + try + { + return Process.GetProcesses(); + } + catch (ArithmeticException ex) // thanks microsoft + { + App.Logger.WriteLine(LOG_IDENT, $"Arithmetic exception occured with System.Diagnostics.Process.GetProcesses: {ex}"); + return Array.Empty(); // can we retry? + } + } } } From 6e022310a59eeb0dce6d4a1bb672ee67db12a060 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Sun, 5 May 2024 11:57:48 +0100 Subject: [PATCH 2/2] GetProcessesSafe: better exception logging --- Bloxstrap/Utilities.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Bloxstrap/Utilities.cs b/Bloxstrap/Utilities.cs index 7bacb94..afc1fb1 100644 --- a/Bloxstrap/Utilities.cs +++ b/Bloxstrap/Utilities.cs @@ -85,7 +85,8 @@ namespace Bloxstrap } catch (ArithmeticException ex) // thanks microsoft { - App.Logger.WriteLine(LOG_IDENT, $"Arithmetic exception occured with System.Diagnostics.Process.GetProcesses: {ex}"); + App.Logger.WriteLine(LOG_IDENT, $"Unable to fetch processes!"); + App.Logger.WriteException(LOG_IDENT, ex); return Array.Empty(); // can we retry? } }