From efba394e5af657f2cb92c669f30015e9da3f0217 Mon Sep 17 00:00:00 2001 From: Ravi Patel Date: Fri, 31 May 2024 17:23:49 +0530 Subject: [PATCH] Now handling all exceptions raised while trying to exit the process. --- AutoUpdater.NET/AutoUpdater.cs | 37 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/AutoUpdater.NET/AutoUpdater.cs b/AutoUpdater.NET/AutoUpdater.cs index 030e45e..685df31 100644 --- a/AutoUpdater.NET/AutoUpdater.cs +++ b/AutoUpdater.NET/AutoUpdater.cs @@ -583,34 +583,33 @@ internal static void Exit() var currentProcess = Process.GetCurrentProcess(); foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName)) { - string processPath; - try - { - processPath = process.MainModule?.FileName; - } - catch (Win32Exception) + if (process.HasExited) { - // Current process should be same as processes created by other instances of the application so it should be able to access modules of other instances. - // This means this is not the process we are looking for so we can safely skip this. continue; } - // Get all instances of assembly except current - if (process.Id == currentProcess.Id || currentProcess.MainModule?.FileName != processPath) + try { - continue; - } + string processPath = process.MainModule?.FileName; - if (process.CloseMainWindow()) - { - process.WaitForExit((int)TimeSpan.FromSeconds(10) - .TotalMilliseconds); // Give some time to process message - } + // Get all instances of assembly except current + if (process.Id == currentProcess.Id || currentProcess.MainModule?.FileName != processPath) + { + continue; + } + + if (process.CloseMainWindow()) + { + process.WaitForExit((int)TimeSpan.FromSeconds(10) + .TotalMilliseconds); // Give some time to process message + } - if (!process.HasExited) - { process.Kill(); //TODO: Show UI message asking user to close program himself instead of silently killing it } + catch (Exception) + { + // ignored + } } if (ApplicationExitEvent != null)