Wrapped try statements around programToStop

If a program starts, and then runs another app and then closes itself, then there is a real possibility that the processToStop logic will fail while trying to handle this process. This code now has 3 try/catch statements around it to try and catch this in a friendly way. This should also highlight any issues with the logic and hopefully make it better.
This commit is contained in:
Terry MacDonald 2021-10-31 10:11:14 +13:00
parent 613283da41
commit 1965ef714f

View File

@ -1816,66 +1816,86 @@ namespace DisplayMagician
foreach (Process processToStop in startProgramsToStop.Reverse<Process>()) foreach (Process processToStop in startProgramsToStop.Reverse<Process>())
{ {
bool stoppedMainProcess = false; bool stoppedMainProcess = false;
// Stop the process if it hasn't stopped already // Stop the process if it hasn't stopped already
if (!processToStop.HasExited) try
{ {
logger.Debug($"ShortcutRepository/RunShortcut: Stopping process {processToStop.StartInfo.FileName}"); if (!processToStop.HasExited)
if (ProcessUtils.StopProcess(processToStop))
{ {
logger.Debug($"ShortcutRepository/RunShortcut: Successfully stopped process {processToStop.StartInfo.FileName}"); logger.Debug($"ShortcutRepository/RunShortcut: Stopping process {processToStop.StartInfo.FileName}");
stoppedMainProcess = true; if (ProcessUtils.StopProcess(processToStop))
{
logger.Debug($"ShortcutRepository/RunShortcut: Successfully stopped process {processToStop.StartInfo.FileName}");
stoppedMainProcess = true;
}
} }
} }
catch (Exception ex)
{
logger.Error(ex, $"ShortcutRepository/RunShortcut: Exception while checking if processToStop has already exited");
}
// Next, check whether it had any other processes it started itself // Next, check whether it had any other processes it started itself
// (copes with loader processes that perform the initial start, then run the main exe) // (copes with loader processes that perform the initial start, then run the main exe)
// If so, we need to go through and find and close all subprocesses // If so, we need to go through and find and close all subprocesses
List<Process> childProcesses = ProcessUtils.FindChildProcesses(processToStop); try
if (childProcesses.Count > 0)
{ {
foreach (Process childProcessToStop in childProcesses) List<Process> childProcesses = ProcessUtils.FindChildProcesses(processToStop);
if (childProcesses.Count > 0)
{ {
if (processToStop.HasExited) foreach (Process childProcessToStop in childProcesses)
{ {
// if there were no child processes, and the only process has already exited (e.g. the user exited it themselves) if (processToStop.HasExited)
// then stop trying to stop the process, and instead log the fact it already stopped. {
Console.WriteLine($"Stopping child process {childProcessToStop.StartInfo.FileName} but was already stopped by user or another process."); // if there were no child processes, and the only process has already exited (e.g. the user exited it themselves)
logger.Warn($"ShortcutRepository/RunShortcut: Stopping child process {childProcessToStop.StartInfo.FileName} but was already stopped by user or another process."); // then stop trying to stop the process, and instead log the fact it already stopped.
continue; Console.WriteLine($"Stopping child process {childProcessToStop.StartInfo.FileName} but was already stopped by user or another process.");
} logger.Warn($"ShortcutRepository/RunShortcut: Stopping child process {childProcessToStop.StartInfo.FileName} but was already stopped by user or another process.");
continue;
}
Console.WriteLine($"Stopping child process {childProcessToStop.StartInfo.FileName} of parent process {processToStop.StartInfo.FileName}"); Console.WriteLine($"Stopping child process {childProcessToStop.StartInfo.FileName} of parent process {processToStop.StartInfo.FileName}");
logger.Debug($"ShortcutRepository/RunShortcut: Stopping child process {childProcessToStop.StartInfo.FileName} of parent process {processToStop.StartInfo.FileName}"); logger.Debug($"ShortcutRepository/RunShortcut: Stopping child process {childProcessToStop.StartInfo.FileName} of parent process {processToStop.StartInfo.FileName}");
ProcessUtils.StopProcess(childProcessToStop); ProcessUtils.StopProcess(childProcessToStop);
}
} }
} }
catch (Exception ex)
{
logger.Error(ex, $"ShortcutRepository/RunShortcut: Exception while checking if processToStop has any child processes");
}
// if the only main process has already exited (e.g. the user exited it themselves) // if the only main process has already exited (e.g. the user exited it themselves)
// then we try to stop any processes with the same name as the application we started // then we try to stop any processes with the same name as the application we started
// Look for the processes with the ProcessName we sorted out earlier // Look for the processes with the ProcessName we sorted out earlier
// Basically, if we haven't stopped all the children processes, then this is the last gasp // Basically, if we haven't stopped all the children processes, then this is the last gasp
if (!stoppedMainProcess) try
{ {
string processName = Path.GetFileNameWithoutExtension(processToStop.StartInfo.FileName); if (!stoppedMainProcess)
List<Process> namedProcessesToStop = Process.GetProcessesByName(processName).ToList(); {
string processName = Path.GetFileNameWithoutExtension(processToStop.StartInfo.FileName);
List<Process> namedProcessesToStop = Process.GetProcessesByName(processName).ToList();
// If we have found one or more processes then we should be good to go // If we have found one or more processes then we should be good to go
if (namedProcessesToStop.Count > 0) if (namedProcessesToStop.Count > 0)
{
logger.Warn($"ShortcutRepository/RunShortcut: We couldn't find any children processes so we've looked for named processes with the name '{processToStop.StartInfo.FileName}' and we found {namedProcessesToStop.Count}. Closing them.");
foreach (Process namedProcessToStop in namedProcessesToStop)
{ {
ProcessUtils.StopProcess(namedProcessToStop); logger.Warn($"ShortcutRepository/RunShortcut: We couldn't find any children processes so we've looked for named processes with the name '{processToStop.StartInfo.FileName}' and we found {namedProcessesToStop.Count}. Closing them.");
foreach (Process namedProcessToStop in namedProcessesToStop)
{
ProcessUtils.StopProcess(namedProcessToStop);
}
} }
else
{
// then give up trying to stop the process, and instead log the fact it already stopped.
Console.WriteLine($"Stopping only process {processToStop.StartInfo.FileName} but was already stopped by user or another process.");
logger.Debug($"ShortcutRepository/RunShortcut: Stopping only process {processToStop.StartInfo.FileName} but was already stopped by user or another process.");
}
continue;
} }
else }
{ catch (Exception ex)
// then give up trying to stop the process, and instead log the fact it already stopped. {
Console.WriteLine($"Stopping only process {processToStop.StartInfo.FileName} but was already stopped by user or another process."); logger.Error(ex, $"ShortcutRepository/RunShortcut: Exception while looking for other processes similar to processToStop for us to stop.");
logger.Debug($"ShortcutRepository/RunShortcut: Stopping only process {processToStop.StartInfo.FileName} but was already stopped by user or another process.");
}
continue;
} }
} }