Skip to content

Commit f6b4810

Browse files
authored
Update Program.cs
1 parent 39db8aa commit f6b4810

1 file changed

Lines changed: 80 additions & 10 deletions

File tree

Program.cs

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public PreventLockApp()
5656
settings = LoadSettings();
5757
isRunning = true;
5858

59-
// Validate and fix startup path if needed (NEW)
59+
// Validate and fix startup path if needed
6060
ValidateAndFixStartupPath();
6161

6262
trayIcon = new NotifyIcon()
@@ -70,7 +70,6 @@ public PreventLockApp()
7070
new Thread(WorkerThreadMethod) { IsBackground = true }.Start();
7171
}
7272

73-
// NEW METHOD: Fixes startup path if executable was moved
7473
private void ValidateAndFixStartupPath()
7574
{
7675
try
@@ -92,7 +91,6 @@ private void ValidateAndFixStartupPath()
9291
catch { /* Silent failure is acceptable */ }
9392
}
9493

95-
/* ALL OTHER METHODS REMAIN EXACTLY THE SAME AS YOUR ORIGINAL CODE */
9694
private AppSettings LoadSettings()
9795
{
9896
try
@@ -379,16 +377,88 @@ protected override void Dispose(bool disposing)
379377

380378
static class Program
381379
{
380+
private static Mutex _mutex;
381+
382382
[STAThread]
383383
static void Main()
384384
{
385-
Application.EnableVisualStyles();
386-
Application.SetCompatibleTextRenderingDefault(false);
387-
388-
// Wait for system tray to initialize
389-
Thread.Sleep(3000);
390-
391-
Application.Run(new PreventLockApp());
385+
const string mutexName = "Global\\SpotifyPreventLock";
386+
bool createdNew;
387+
_mutex = new Mutex(true, mutexName, out createdNew);
388+
389+
if (!createdNew)
390+
{
391+
Version currentVersion = Assembly.GetExecutingAssembly().GetName().Version;
392+
Process currentProcess = Process.GetCurrentProcess();
393+
394+
foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
395+
{
396+
try
397+
{
398+
if (process.Id == currentProcess.Id) continue;
399+
400+
string processPath = process.MainModule?.FileName;
401+
if (string.IsNullOrEmpty(processPath)) continue;
402+
403+
Version runningVersion = AssemblyName.GetAssemblyName(processPath).Version;
404+
405+
if (runningVersion > currentVersion)
406+
{
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);
412+
return;
413+
}
414+
else if (runningVersion < currentVersion)
415+
{
416+
var result = MessageBox.Show($"An older version (v{runningVersion.ToString(3)}) is running.\n\n" +
417+
$"Current version: v{currentVersion.ToString(3)}\n\n" +
418+
"Would you like to close the old version and launch this new version?",
419+
"New Version Available",
420+
MessageBoxButtons.YesNo,
421+
MessageBoxIcon.Question);
422+
423+
if (result == DialogResult.Yes)
424+
{
425+
try
426+
{
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);
431+
}
432+
catch
433+
{
434+
MessageBox.Show("Failed to close the previous version.",
435+
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
436+
}
437+
}
438+
return;
439+
}
440+
else
441+
{
442+
MessageBox.Show("Spotify Prevent Lock is already running.",
443+
"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
444+
return;
445+
}
446+
}
447+
catch { /* Ignore processes we can't access */ }
448+
}
449+
return;
450+
}
451+
452+
try
453+
{
454+
Application.EnableVisualStyles();
455+
Application.SetCompatibleTextRenderingDefault(false);
456+
Application.Run(new PreventLockApp());
457+
}
458+
finally
459+
{
460+
_mutex?.ReleaseMutex();
461+
}
392462
}
393463
}
394464
}

0 commit comments

Comments
 (0)