Merge pull request #1739 from bluepilledgreat/bugfix/getprocesses

Safer GetProcesses
This commit is contained in:
pizzaboxer 2024-05-12 20:54:27 +01:00 committed by GitHub
commit 4e379dd0f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 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

@ -420,7 +420,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,21 @@ 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, $"Unable to fetch processes!");
App.Logger.WriteException(LOG_IDENT, ex);
return Array.Empty<Process>(); // can we retry?
}
}
}
}