Skip to content

Commit 973de00

Browse files
authored
Update Program.cs
1 parent 51b1978 commit 973de00

1 file changed

Lines changed: 49 additions & 25 deletions

File tree

Program.cs

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ private enum ExecutionState : uint
4242

4343
public PreventLockApp()
4444
{
45-
// Safe version initialization with null checks
4645
var version = Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0);
4746
appVersion = $"v{version.Major}.{version.Minor}.{version.Build}";
4847
versionFont = new Font("Segoe UI", 8.25f, FontStyle.Italic);
@@ -56,7 +55,6 @@ public PreventLockApp()
5655
settings = LoadSettings();
5756
isRunning = true;
5857

59-
// Validate and fix startup path if needed
6058
ValidateAndFixStartupPath();
6159

6260
trayIcon = new NotifyIcon()
@@ -401,21 +399,29 @@ static void Main()
401399
if (string.IsNullOrEmpty(processPath)) continue;
402400

403401
var runningVersion = AssemblyName.GetAssemblyName(processPath).Version ?? new Version(1, 0, 0);
404-
405-
if (runningVersion > currentVersion)
402+
int versionComparison = runningVersion.CompareTo(currentVersion);
403+
404+
if (versionComparison == 0) // Same version
406405
{
407-
MessageBox.Show($"A newer version (v{runningVersion.ToString(3)}) is already running.\n\n" +
408-
$"Please close this version (v{currentVersion.ToString(3)}) and use the newer version.",
409-
"Newer Version Running",
410-
MessageBoxButtons.OK,
411-
MessageBoxIcon.Information);
406+
MessageBox.Show("Spotify Prevent Lock is already running",
407+
"Information",
408+
MessageBoxButtons.OK,
409+
MessageBoxIcon.Information);
410+
return;
411+
}
412+
else if (versionComparison > 0) // Newer version running
413+
{
414+
MessageBox.Show($"A newer version (v{runningVersion.ToString(3)}) is already running",
415+
"Information",
416+
MessageBoxButtons.OK,
417+
MessageBoxIcon.Information);
412418
return;
413419
}
414-
else if (runningVersion < currentVersion)
420+
else // Older version running - offer upgrade
415421
{
416-
var result = MessageBox.Show($"An older version (v{runningVersion.ToString(3)}) is running.\n\n" +
422+
var result = MessageBox.Show($"An older version is running (v{runningVersion.ToString(3)}).\n\n" +
417423
$"Current version: v{currentVersion.ToString(3)}\n\n" +
418-
"Would you like to close the old version and launch this new version?",
424+
"Would you like to upgrade to the new version?",
419425
"New Version Available",
420426
MessageBoxButtons.YesNo,
421427
MessageBoxIcon.Question);
@@ -424,25 +430,43 @@ static void Main()
424430
{
425431
try
426432
{
427-
process.Kill();
428-
process.WaitForExit(3000); // Wait up to 3 seconds
429-
Thread.Sleep(500); // Small delay before restarting
430-
Process.Start(Application.ExecutablePath);
433+
// Try to close gracefully first
434+
if (!process.CloseMainWindow())
435+
{
436+
process.Kill();
437+
}
438+
439+
// Wait for process to exit
440+
if (!process.WaitForExit(5000))
441+
{
442+
MessageBox.Show("The previous version didn't close properly.\nPlease close it manually and try again.",
443+
"Upgrade Warning",
444+
MessageBoxButtons.OK,
445+
MessageBoxIcon.Warning);
446+
return;
447+
}
448+
449+
// Brief pause before launching new version
450+
Thread.Sleep(300);
451+
452+
// Launch new version automatically
453+
Process.Start(new ProcessStartInfo
454+
{
455+
FileName = Application.ExecutablePath,
456+
UseShellExecute = true,
457+
Verb = "open"
458+
});
431459
}
432-
catch
460+
catch (Exception ex)
433461
{
434-
MessageBox.Show("Failed to close the previous version.",
435-
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
462+
MessageBox.Show($"Failed to upgrade: {ex.Message}\n\nPlease close the old version manually and try again.",
463+
"Upgrade Error",
464+
MessageBoxButtons.OK,
465+
MessageBoxIcon.Error);
436466
}
437467
}
438468
return;
439469
}
440-
else
441-
{
442-
MessageBox.Show("Spotify Prevent Lock is already running.",
443-
"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
444-
return;
445-
}
446470
}
447471
catch { /* Ignore processes we can't access */ }
448472
}

0 commit comments

Comments
 (0)