safer getprocesses

thanks microsoft
This commit is contained in:
bluepilledgreat 2024-05-04 12:40:43 +01:00
parent c386695078
commit 6e95046af2
3 changed files with 17 additions and 2 deletions

View File

@ -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)
{

View File

@ -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");

View File

@ -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<Process>(); // can we retry?
}
}
}
}