Skip to content

Commit

Permalink
Now handling all exceptions raised while trying to exit the process.
Browse files Browse the repository at this point in the history
  • Loading branch information
ravibpatel committed May 31, 2024
1 parent 86ae08d commit efba394
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions AutoUpdater.NET/AutoUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit efba394

Please sign in to comment.