From f6b4810a1994e6a9cadcfeb770ecc3ea860ee6fd Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Fri, 1 Aug 2025 23:44:36 +0800 Subject: [PATCH 01/18] Update Program.cs --- Program.cs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 80 insertions(+), 10 deletions(-) diff --git a/Program.cs b/Program.cs index 39106f9..8338247 100644 --- a/Program.cs +++ b/Program.cs @@ -56,7 +56,7 @@ public PreventLockApp() settings = LoadSettings(); isRunning = true; - // Validate and fix startup path if needed (NEW) + // Validate and fix startup path if needed ValidateAndFixStartupPath(); trayIcon = new NotifyIcon() @@ -70,7 +70,6 @@ public PreventLockApp() new Thread(WorkerThreadMethod) { IsBackground = true }.Start(); } - // NEW METHOD: Fixes startup path if executable was moved private void ValidateAndFixStartupPath() { try @@ -92,7 +91,6 @@ private void ValidateAndFixStartupPath() catch { /* Silent failure is acceptable */ } } - /* ALL OTHER METHODS REMAIN EXACTLY THE SAME AS YOUR ORIGINAL CODE */ private AppSettings LoadSettings() { try @@ -379,16 +377,88 @@ protected override void Dispose(bool disposing) static class Program { + private static Mutex _mutex; + [STAThread] static void Main() { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - // Wait for system tray to initialize - Thread.Sleep(3000); - - Application.Run(new PreventLockApp()); + const string mutexName = "Global\\SpotifyPreventLock"; + bool createdNew; + _mutex = new Mutex(true, mutexName, out createdNew); + + if (!createdNew) + { + Version currentVersion = Assembly.GetExecutingAssembly().GetName().Version; + Process currentProcess = Process.GetCurrentProcess(); + + foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName)) + { + try + { + if (process.Id == currentProcess.Id) continue; + + string processPath = process.MainModule?.FileName; + if (string.IsNullOrEmpty(processPath)) continue; + + Version runningVersion = AssemblyName.GetAssemblyName(processPath).Version; + + if (runningVersion > currentVersion) + { + MessageBox.Show($"A newer version (v{runningVersion.ToString(3)}) is already running.\n\n" + + $"Please close this version (v{currentVersion.ToString(3)}) and use the newer version.", + "Newer Version Running", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + return; + } + else if (runningVersion < currentVersion) + { + var result = MessageBox.Show($"An older version (v{runningVersion.ToString(3)}) is running.\n\n" + + $"Current version: v{currentVersion.ToString(3)}\n\n" + + "Would you like to close the old version and launch this new version?", + "New Version Available", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question); + + if (result == DialogResult.Yes) + { + try + { + process.Kill(); + process.WaitForExit(3000); // Wait up to 3 seconds + Thread.Sleep(500); // Small delay before restarting + Process.Start(Application.ExecutablePath); + } + catch + { + MessageBox.Show("Failed to close the previous version.", + "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + return; + } + else + { + MessageBox.Show("Spotify Prevent Lock is already running.", + "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + } + catch { /* Ignore processes we can't access */ } + } + return; + } + + try + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new PreventLockApp()); + } + finally + { + _mutex?.ReleaseMutex(); + } } } } From 514f77bab18850db6a2b89e016b7d475d6ddc263 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Fri, 1 Aug 2025 23:53:39 +0800 Subject: [PATCH 02/18] Update Program.cs --- Program.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Program.cs b/Program.cs index 8338247..33e85f2 100644 --- a/Program.cs +++ b/Program.cs @@ -377,7 +377,7 @@ protected override void Dispose(bool disposing) static class Program { - private static Mutex _mutex; + private static Mutex? _mutex; [STAThread] static void Main() @@ -388,19 +388,19 @@ static void Main() if (!createdNew) { - Version currentVersion = Assembly.GetExecutingAssembly().GetName().Version; - Process currentProcess = Process.GetCurrentProcess(); + var currentVersion = Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0); + var currentProcess = Process.GetCurrentProcess(); - foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName)) + foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName)) { try { if (process.Id == currentProcess.Id) continue; - string processPath = process.MainModule?.FileName; + string? processPath = process.MainModule?.FileName; if (string.IsNullOrEmpty(processPath)) continue; - Version runningVersion = AssemblyName.GetAssemblyName(processPath).Version; + var runningVersion = AssemblyName.GetAssemblyName(processPath).Version ?? new Version(1, 0, 0); if (runningVersion > currentVersion) { From 51b197862f0490cc0e967c1b12a32d55d8cc3480 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Fri, 1 Aug 2025 23:56:58 +0800 Subject: [PATCH 03/18] Update SpotifyPreventLock.csproj --- SpotifyPreventLock.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpotifyPreventLock.csproj b/SpotifyPreventLock.csproj index 85040b9..ba9458c 100644 --- a/SpotifyPreventLock.csproj +++ b/SpotifyPreventLock.csproj @@ -8,7 +8,7 @@ true - 1.1.0 + 1.1.1 dev From 973de008183acd46bbbe4238b04fd38ef47cebcd Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 00:12:13 +0800 Subject: [PATCH 04/18] Update Program.cs --- Program.cs | 74 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/Program.cs b/Program.cs index 33e85f2..2096d69 100644 --- a/Program.cs +++ b/Program.cs @@ -42,7 +42,6 @@ private enum ExecutionState : uint public PreventLockApp() { - // Safe version initialization with null checks var version = Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0); appVersion = $"v{version.Major}.{version.Minor}.{version.Build}"; versionFont = new Font("Segoe UI", 8.25f, FontStyle.Italic); @@ -56,7 +55,6 @@ public PreventLockApp() settings = LoadSettings(); isRunning = true; - // Validate and fix startup path if needed ValidateAndFixStartupPath(); trayIcon = new NotifyIcon() @@ -401,21 +399,29 @@ static void Main() if (string.IsNullOrEmpty(processPath)) continue; var runningVersion = AssemblyName.GetAssemblyName(processPath).Version ?? new Version(1, 0, 0); - - if (runningVersion > currentVersion) + int versionComparison = runningVersion.CompareTo(currentVersion); + + if (versionComparison == 0) // Same version { - MessageBox.Show($"A newer version (v{runningVersion.ToString(3)}) is already running.\n\n" + - $"Please close this version (v{currentVersion.ToString(3)}) and use the newer version.", - "Newer Version Running", - MessageBoxButtons.OK, - MessageBoxIcon.Information); + MessageBox.Show("Spotify Prevent Lock is already running", + "Information", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + return; + } + else if (versionComparison > 0) // Newer version running + { + MessageBox.Show($"A newer version (v{runningVersion.ToString(3)}) is already running", + "Information", + MessageBoxButtons.OK, + MessageBoxIcon.Information); return; } - else if (runningVersion < currentVersion) + else // Older version running - offer upgrade { - var result = MessageBox.Show($"An older version (v{runningVersion.ToString(3)}) is running.\n\n" + + var result = MessageBox.Show($"An older version is running (v{runningVersion.ToString(3)}).\n\n" + $"Current version: v{currentVersion.ToString(3)}\n\n" + - "Would you like to close the old version and launch this new version?", + "Would you like to upgrade to the new version?", "New Version Available", MessageBoxButtons.YesNo, MessageBoxIcon.Question); @@ -424,25 +430,43 @@ static void Main() { try { - process.Kill(); - process.WaitForExit(3000); // Wait up to 3 seconds - Thread.Sleep(500); // Small delay before restarting - Process.Start(Application.ExecutablePath); + // Try to close gracefully first + if (!process.CloseMainWindow()) + { + process.Kill(); + } + + // Wait for process to exit + if (!process.WaitForExit(5000)) + { + MessageBox.Show("The previous version didn't close properly.\nPlease close it manually and try again.", + "Upgrade Warning", + MessageBoxButtons.OK, + MessageBoxIcon.Warning); + return; + } + + // Brief pause before launching new version + Thread.Sleep(300); + + // Launch new version automatically + Process.Start(new ProcessStartInfo + { + FileName = Application.ExecutablePath, + UseShellExecute = true, + Verb = "open" + }); } - catch + catch (Exception ex) { - MessageBox.Show("Failed to close the previous version.", - "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Failed to upgrade: {ex.Message}\n\nPlease close the old version manually and try again.", + "Upgrade Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); } } return; } - else - { - MessageBox.Show("Spotify Prevent Lock is already running.", - "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); - return; - } } catch { /* Ignore processes we can't access */ } } From e655f566cf5019233fcb94ee01bf268de89e9e14 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 00:49:37 +0800 Subject: [PATCH 05/18] Update Program.cs --- Program.cs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Program.cs b/Program.cs index 2096d69..3702740 100644 --- a/Program.cs +++ b/Program.cs @@ -411,18 +411,19 @@ static void Main() } else if (versionComparison > 0) // Newer version running { - MessageBox.Show($"A newer version (v{runningVersion.ToString(3)}) is already running", - "Information", + MessageBox.Show($"A newer version (v{runningVersion.ToString(3)}) is already running\n\n" + + "Please close this version and use the newer version.", + "New Version Detected", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } - else // Older version running - offer upgrade + else // Older version running { var result = MessageBox.Show($"An older version is running (v{runningVersion.ToString(3)}).\n\n" + $"Current version: v{currentVersion.ToString(3)}\n\n" + - "Would you like to upgrade to the new version?", - "New Version Available", + "Would you like to close the old version and upgrade now?", + "Update Available", MessageBoxButtons.YesNo, MessageBoxIcon.Question); @@ -437,30 +438,29 @@ static void Main() } // Wait for process to exit - if (!process.WaitForExit(5000)) + if (!process.WaitForExit(3000)) { - MessageBox.Show("The previous version didn't close properly.\nPlease close it manually and try again.", - "Upgrade Warning", + MessageBox.Show("Could not close the previous version.\nPlease close it manually and run the new version again.", + "Update Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Brief pause before launching new version - Thread.Sleep(300); + Thread.Sleep(500); // Launch new version automatically Process.Start(new ProcessStartInfo { FileName = Application.ExecutablePath, - UseShellExecute = true, - Verb = "open" + UseShellExecute = true }); } catch (Exception ex) { - MessageBox.Show($"Failed to upgrade: {ex.Message}\n\nPlease close the old version manually and try again.", - "Upgrade Error", + MessageBox.Show($"Failed to upgrade: {ex.Message}\n\nPlease close the old version manually and run the new version again.", + "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } From 24a0b676a4673310d1658d6cfd6670d04d0811f3 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 00:55:02 +0800 Subject: [PATCH 06/18] Update SpotifyPreventLock.csproj --- SpotifyPreventLock.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpotifyPreventLock.csproj b/SpotifyPreventLock.csproj index ba9458c..b7236c8 100644 --- a/SpotifyPreventLock.csproj +++ b/SpotifyPreventLock.csproj @@ -8,7 +8,7 @@ true - 1.1.1 + 1.1.2 dev From b1f2a1f5084dbee2ec0d70daf38585db20963b54 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 01:53:55 +0800 Subject: [PATCH 07/18] Update Program.cs --- Program.cs | 193 +++++++++++++++++++++++++++++------------------------ 1 file changed, 107 insertions(+), 86 deletions(-) diff --git a/Program.cs b/Program.cs index 3702740..dfb0cbd 100644 --- a/Program.cs +++ b/Program.cs @@ -380,109 +380,130 @@ static class Program [STAThread] static void Main() { + // Create application mutex to prevent multiple instances const string mutexName = "Global\\SpotifyPreventLock"; - bool createdNew; - _mutex = new Mutex(true, mutexName, out createdNew); + _mutex = new Mutex(true, mutexName, out bool isFirstInstance); - if (!createdNew) + if (!isFirstInstance) { - var currentVersion = Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0); - var currentProcess = Process.GetCurrentProcess(); - - foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName)) + ShowRunningInstanceWarning(); + return; + } + + try + { + // Check for version conflicts before running + if (CheckForVersionConflicts()) + { + return; + } + + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new PreventLockApp()); + } + finally + { + _mutex?.ReleaseMutex(); + _mutex?.Dispose(); + } + } + + private static void ShowRunningInstanceWarning() + { + MessageBox.Show( + "Spotify Prevent Lock is already running.\n\n" + + "Please check your system tray or task manager.", + "Already Running", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + } + + private static bool CheckForVersionConflicts() + { + var currentProcess = Process.GetCurrentProcess(); + var currentVersion = Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0); + + foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName)) + { + try { - try + if (process.Id == currentProcess.Id) continue; + + string? processPath = process.MainModule?.FileName; + if (string.IsNullOrEmpty(processPath)) continue; + + var runningVersion = FileVersionInfo.GetVersionInfo(processPath).FileVersion != null + ? new Version(FileVersionInfo.GetVersionInfo(processPath).FileVersion) + : new Version(1, 0, 0); + + int versionComparison = runningVersion.CompareTo(currentVersion); + + if (versionComparison > 0) // Newer version running { - if (process.Id == currentProcess.Id) continue; - - string? processPath = process.MainModule?.FileName; - if (string.IsNullOrEmpty(processPath)) continue; - - var runningVersion = AssemblyName.GetAssemblyName(processPath).Version ?? new Version(1, 0, 0); - int versionComparison = runningVersion.CompareTo(currentVersion); - - if (versionComparison == 0) // Same version - { - MessageBox.Show("Spotify Prevent Lock is already running", - "Information", - MessageBoxButtons.OK, - MessageBoxIcon.Information); - return; - } - else if (versionComparison > 0) // Newer version running - { - MessageBox.Show($"A newer version (v{runningVersion.ToString(3)}) is already running\n\n" + - "Please close this version and use the newer version.", - "New Version Detected", - MessageBoxButtons.OK, - MessageBoxIcon.Information); - return; - } - else // Older version running + MessageBox.Show( + $"A newer version (v{runningVersion}) is already running!\n\n" + + "Please close this version and use the newer one.", + "New Version Detected", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + return true; + } + else if (versionComparison < 0) // Older version running + { + var result = MessageBox.Show( + $"An older version (v{runningVersion}) is running.\n\n" + + $"You're trying to launch version v{currentVersion}.\n\n" + + "Would you like to close the old version and upgrade?", + "Upgrade Available", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question); + + if (result == DialogResult.Yes) { - var result = MessageBox.Show($"An older version is running (v{runningVersion.ToString(3)}).\n\n" + - $"Current version: v{currentVersion.ToString(3)}\n\n" + - "Would you like to close the old version and upgrade now?", - "Update Available", - MessageBoxButtons.YesNo, - MessageBoxIcon.Question); - - if (result == DialogResult.Yes) + try { - try + if (!process.CloseMainWindow()) { - // Try to close gracefully first - if (!process.CloseMainWindow()) - { - process.Kill(); - } - - // Wait for process to exit - if (!process.WaitForExit(3000)) - { - MessageBox.Show("Could not close the previous version.\nPlease close it manually and run the new version again.", - "Update Warning", - MessageBoxButtons.OK, - MessageBoxIcon.Warning); - return; - } - - // Brief pause before launching new version - Thread.Sleep(500); - - // Launch new version automatically - Process.Start(new ProcessStartInfo - { - FileName = Application.ExecutablePath, - UseShellExecute = true - }); + process.Kill(); } - catch (Exception ex) + + if (!process.WaitForExit(3000)) { - MessageBox.Show($"Failed to upgrade: {ex.Message}\n\nPlease close the old version manually and run the new version again.", - "Update Error", + MessageBox.Show( + "Could not close the previous version.\n" + + "Please close it manually and try again.", + "Upgrade Warning", MessageBoxButtons.OK, - MessageBoxIcon.Error); + MessageBoxIcon.Warning); + return true; } + + // Restart the application + Process.Start(new ProcessStartInfo + { + FileName = Application.ExecutablePath, + UseShellExecute = true + }); + return true; + } + catch (Exception ex) + { + MessageBox.Show( + $"Failed to upgrade: {ex.Message}\n\n" + + "Please close the old version manually and try again.", + "Upgrade Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + return true; } - return; } + return true; } - catch { /* Ignore processes we can't access */ } } - return; - } - - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new PreventLockApp()); - } - finally - { - _mutex?.ReleaseMutex(); + catch { /* Ignore processes we can't access */ } } + return false; } } } From 1073df78457e063f6513681a1d676a16ffc897b6 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 02:09:59 +0800 Subject: [PATCH 08/18] Update Program.cs --- Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Program.cs b/Program.cs index dfb0cbd..bdcf7dd 100644 --- a/Program.cs +++ b/Program.cs @@ -380,7 +380,6 @@ static class Program [STAThread] static void Main() { - // Create application mutex to prevent multiple instances const string mutexName = "Global\\SpotifyPreventLock"; _mutex = new Mutex(true, mutexName, out bool isFirstInstance); @@ -433,8 +432,9 @@ private static bool CheckForVersionConflicts() string? processPath = process.MainModule?.FileName; if (string.IsNullOrEmpty(processPath)) continue; - var runningVersion = FileVersionInfo.GetVersionInfo(processPath).FileVersion != null - ? new Version(FileVersionInfo.GetVersionInfo(processPath).FileVersion) + var fileVersionInfo = FileVersionInfo.GetVersionInfo(processPath); + var runningVersion = !string.IsNullOrEmpty(fileVersionInfo.FileVersion) + ? new Version(fileVersionInfo.FileVersion) : new Version(1, 0, 0); int versionComparison = runningVersion.CompareTo(currentVersion); From a4e18b0826b2a2b0ed12809ae8aca91696993431 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 02:16:30 +0800 Subject: [PATCH 09/18] Update SpotifyPreventLock.csproj --- SpotifyPreventLock.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpotifyPreventLock.csproj b/SpotifyPreventLock.csproj index b7236c8..ba9458c 100644 --- a/SpotifyPreventLock.csproj +++ b/SpotifyPreventLock.csproj @@ -8,7 +8,7 @@ true - 1.1.2 + 1.1.1 dev From d881ee9fa50133f41a72ff471ac2e486d44439f7 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 02:37:37 +0800 Subject: [PATCH 10/18] Update Program.cs --- Program.cs | 178 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 108 insertions(+), 70 deletions(-) diff --git a/Program.cs b/Program.cs index bdcf7dd..825f32e 100644 --- a/Program.cs +++ b/Program.cs @@ -376,14 +376,16 @@ protected override void Dispose(bool disposing) static class Program { private static Mutex? _mutex; + private const string AppName = "Spotify Prevent Lock"; [STAThread] static void Main() { const string mutexName = "Global\\SpotifyPreventLock"; - _mutex = new Mutex(true, mutexName, out bool isFirstInstance); + bool createdNew; + _mutex = new Mutex(true, mutexName, out createdNew); - if (!isFirstInstance) + if (!createdNew) { ShowRunningInstanceWarning(); return; @@ -391,9 +393,18 @@ static void Main() try { - // Check for version conflicts before running - if (CheckForVersionConflicts()) + var versionCheck = CheckRunningVersions(); + if (versionCheck != VersionCheckResult.Continue) { + if (versionCheck == VersionCheckResult.Restart) + { + Thread.Sleep(500); // Brief pause before restart + Process.Start(new ProcessStartInfo + { + FileName = Application.ExecutablePath, + UseShellExecute = true + }); + } return; } @@ -408,20 +419,27 @@ static void Main() } } + private enum VersionCheckResult + { + Continue, + Exit, + Restart + } + private static void ShowRunningInstanceWarning() { MessageBox.Show( - "Spotify Prevent Lock is already running.\n\n" + + $"{AppName} is already running.\n\n" + "Please check your system tray or task manager.", - "Already Running", + "Application Running", MessageBoxButtons.OK, MessageBoxIcon.Information); } - private static bool CheckForVersionConflicts() + private static VersionCheckResult CheckRunningVersions() { var currentProcess = Process.GetCurrentProcess(); - var currentVersion = Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0); + var currentVersion = GetCurrentVersion(); foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName)) { @@ -429,81 +447,101 @@ private static bool CheckForVersionConflicts() { if (process.Id == currentProcess.Id) continue; - string? processPath = process.MainModule?.FileName; - if (string.IsNullOrEmpty(processPath)) continue; + var runningVersion = GetProcessVersion(process); + if (runningVersion == null) continue; - var fileVersionInfo = FileVersionInfo.GetVersionInfo(processPath); - var runningVersion = !string.IsNullOrEmpty(fileVersionInfo.FileVersion) - ? new Version(fileVersionInfo.FileVersion) - : new Version(1, 0, 0); + int comparison = runningVersion.CompareTo(currentVersion); - int versionComparison = runningVersion.CompareTo(currentVersion); - - if (versionComparison > 0) // Newer version running + if (comparison > 0) // Newer version running { MessageBox.Show( $"A newer version (v{runningVersion}) is already running!\n\n" + - "Please close this version and use the newer one.", + $"Please close this version (v{currentVersion}) and use the newer one.", "New Version Detected", MessageBoxButtons.OK, MessageBoxIcon.Information); - return true; + return VersionCheckResult.Exit; } - else if (versionComparison < 0) // Older version running + else if (comparison < 0) // Older version running { - var result = MessageBox.Show( - $"An older version (v{runningVersion}) is running.\n\n" + - $"You're trying to launch version v{currentVersion}.\n\n" + - "Would you like to close the old version and upgrade?", - "Upgrade Available", - MessageBoxButtons.YesNo, - MessageBoxIcon.Question); - - if (result == DialogResult.Yes) - { - try - { - if (!process.CloseMainWindow()) - { - process.Kill(); - } - - if (!process.WaitForExit(3000)) - { - MessageBox.Show( - "Could not close the previous version.\n" + - "Please close it manually and try again.", - "Upgrade Warning", - MessageBoxButtons.OK, - MessageBoxIcon.Warning); - return true; - } - - // Restart the application - Process.Start(new ProcessStartInfo - { - FileName = Application.ExecutablePath, - UseShellExecute = true - }); - return true; - } - catch (Exception ex) - { - MessageBox.Show( - $"Failed to upgrade: {ex.Message}\n\n" + - "Please close the old version manually and try again.", - "Upgrade Error", - MessageBoxButtons.OK, - MessageBoxIcon.Error); - return true; - } - } - return true; + return HandleOlderVersion(process, currentVersion, runningVersion); + } + else // Same version running + { + return VersionCheckResult.Exit; } } - catch { /* Ignore processes we can't access */ } + catch { /* Ignore inaccessible processes */ } + } + return VersionCheckResult.Continue; + } + + private static Version GetCurrentVersion() + { + return Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0); + } + + private static Version? GetProcessVersion(Process process) + { + try + { + string? path = process.MainModule?.FileName; + if (string.IsNullOrEmpty(path)) return null; + + var versionInfo = FileVersionInfo.GetVersionInfo(path); + return string.IsNullOrEmpty(versionInfo.FileVersion) + ? null + : new Version(versionInfo.FileVersion); + } + catch + { + return null; + } + } + + private static VersionCheckResult HandleOlderVersion(Process process, Version currentVersion, Version runningVersion) + { + var result = MessageBox.Show( + $"An older version (v{runningVersion}) is running.\n\n" + + $"Current version: v{currentVersion}\n\n" + + "Would you like to upgrade now?", + "Upgrade Available", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question); + + if (result != DialogResult.Yes) return VersionCheckResult.Exit; + + try + { + // Try graceful shutdown first + if (!process.CloseMainWindow()) + { + process.Kill(); + } + + if (!process.WaitForExit(3000)) + { + MessageBox.Show( + "Could not close the previous version.\n" + + "Please close it manually and run the new version again.", + "Upgrade Warning", + MessageBoxButtons.OK, + MessageBoxIcon.Warning); + return VersionCheckResult.Exit; + } + + return VersionCheckResult.Restart; + } + catch (Exception ex) + { + MessageBox.Show( + $"Upgrade failed: {ex.Message}\n\n" + + "Please close the old version manually and try again.", + "Upgrade Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + return VersionCheckResult.Exit; } - return false; } } } From c2d0990d5e96c4b4ef16ee96653b95a31d8f069e Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 02:40:59 +0800 Subject: [PATCH 11/18] Update SpotifyPreventLock.csproj --- SpotifyPreventLock.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpotifyPreventLock.csproj b/SpotifyPreventLock.csproj index ba9458c..b7236c8 100644 --- a/SpotifyPreventLock.csproj +++ b/SpotifyPreventLock.csproj @@ -8,7 +8,7 @@ true - 1.1.1 + 1.1.2 dev From 00584763bf0056a4f465bf858319810b4b718796 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 03:00:32 +0800 Subject: [PATCH 12/18] Update Program.cs --- Program.cs | 135 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 110 insertions(+), 25 deletions(-) diff --git a/Program.cs b/Program.cs index 825f32e..b4f2bdc 100644 --- a/Program.cs +++ b/Program.cs @@ -8,6 +8,7 @@ using System.Diagnostics; using System.Reflection; using Microsoft.Win32; +using System.Linq; namespace SpotifyPreventLock { @@ -375,24 +376,25 @@ protected override void Dispose(bool disposing) static class Program { - private static Mutex? _mutex; + private static Mutex? _appMutex; + private static readonly Version CurrentVersion = Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0); private const string AppName = "Spotify Prevent Lock"; [STAThread] static void Main() { - const string mutexName = "Global\\SpotifyPreventLock"; - bool createdNew; - _mutex = new Mutex(true, mutexName, out createdNew); - - if (!createdNew) - { - ShowRunningInstanceWarning(); - return; - } + const string mutexName = @"Global\SpotifyPreventLock_{B5FE0EF1-6D4B-4A9B-9B9D-8A1D8B7C8D9F}"; try { + _appMutex = new Mutex(true, mutexName, out bool isFirstInstance); + + if (!isFirstInstance) + { + HandleExistingInstance(); + return; + } + var versionCheck = CheckRunningVersions(); if (versionCheck != VersionCheckResult.Continue) { @@ -414,8 +416,8 @@ static void Main() } finally { - _mutex?.ReleaseMutex(); - _mutex?.Dispose(); + _appMutex?.ReleaseMutex(); + _appMutex?.Dispose(); } } @@ -426,14 +428,103 @@ private enum VersionCheckResult Restart } - private static void ShowRunningInstanceWarning() + private static void HandleExistingInstance() { - MessageBox.Show( - $"{AppName} is already running.\n\n" + - "Please check your system tray or task manager.", - "Application Running", - MessageBoxButtons.OK, - MessageBoxIcon.Information); + var existingProcesses = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName) + .Where(p => p.Id != Process.GetCurrentProcess().Id) + .ToList(); + + if (existingProcesses.Count == 0) return; + + var existingVersion = GetRunningVersion(existingProcesses[0]); + int versionComparison = existingVersion.CompareTo(CurrentVersion); + + if (versionComparison == 0) // Same version + { + MessageBox.Show($"{AppName} is already running.\n\n" + + "Please check your system tray or task manager.", + "Information", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + } + else if (versionComparison > 0) // Newer version running + { + MessageBox.Show($"A newer version (v{existingVersion}) is already running!\n\n" + + $"Please close this version (v{CurrentVersion}) and use the newer one.", + "New Version Detected", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + } + else // Older version running + { + var result = MessageBox.Show( + $"An older version (v{existingVersion}) is running.\n\n" + + $"Current version: v{CurrentVersion}\n\n" + + "Would you like to upgrade now?", + "Upgrade Available", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question); + + if (result == DialogResult.Yes) + { + try + { + var process = existingProcesses[0]; + // Try graceful shutdown first + if (!process.CloseMainWindow()) + { + process.Kill(); + } + + if (!process.WaitForExit(3000)) + { + MessageBox.Show( + "Could not close the previous version.\n" + + "Please close it manually and run the new version again.", + "Upgrade Warning", + MessageBoxButtons.OK, + MessageBoxIcon.Warning); + return; + } + + Thread.Sleep(500); // Brief pause before restart + Process.Start(new ProcessStartInfo + { + FileName = Application.ExecutablePath, + UseShellExecute = true + }); + } + catch (Exception ex) + { + MessageBox.Show( + $"Upgrade failed: {ex.Message}\n\n" + + "Please close the old version manually and try again.", + "Upgrade Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + } + + private static Version GetRunningVersion(Process process) + { + try + { + if (process.MainModule?.FileName is string fileName) + { + var versionInfo = FileVersionInfo.GetVersionInfo(fileName); + if (versionInfo.FileVersion != null) + { + return new Version(versionInfo.FileVersion); + } + } + } + catch + { + // Fallback if version can't be determined + } + return new Version(0, 0); } private static VersionCheckResult CheckRunningVersions() @@ -454,12 +545,6 @@ private static VersionCheckResult CheckRunningVersions() if (comparison > 0) // Newer version running { - MessageBox.Show( - $"A newer version (v{runningVersion}) is already running!\n\n" + - $"Please close this version (v{currentVersion}) and use the newer one.", - "New Version Detected", - MessageBoxButtons.OK, - MessageBoxIcon.Information); return VersionCheckResult.Exit; } else if (comparison < 0) // Older version running From dfd54a13cb60086be2e50af6feb68af3b38bb512 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 03:06:43 +0800 Subject: [PATCH 13/18] Update Program.cs --- Program.cs | 135 ++++++++++------------------------------------------- 1 file changed, 25 insertions(+), 110 deletions(-) diff --git a/Program.cs b/Program.cs index b4f2bdc..825f32e 100644 --- a/Program.cs +++ b/Program.cs @@ -8,7 +8,6 @@ using System.Diagnostics; using System.Reflection; using Microsoft.Win32; -using System.Linq; namespace SpotifyPreventLock { @@ -376,25 +375,24 @@ protected override void Dispose(bool disposing) static class Program { - private static Mutex? _appMutex; - private static readonly Version CurrentVersion = Assembly.GetExecutingAssembly().GetName().Version ?? new Version(1, 0, 0); + private static Mutex? _mutex; private const string AppName = "Spotify Prevent Lock"; [STAThread] static void Main() { - const string mutexName = @"Global\SpotifyPreventLock_{B5FE0EF1-6D4B-4A9B-9B9D-8A1D8B7C8D9F}"; + const string mutexName = "Global\\SpotifyPreventLock"; + bool createdNew; + _mutex = new Mutex(true, mutexName, out createdNew); - try + if (!createdNew) { - _appMutex = new Mutex(true, mutexName, out bool isFirstInstance); - - if (!isFirstInstance) - { - HandleExistingInstance(); - return; - } + ShowRunningInstanceWarning(); + return; + } + try + { var versionCheck = CheckRunningVersions(); if (versionCheck != VersionCheckResult.Continue) { @@ -416,8 +414,8 @@ static void Main() } finally { - _appMutex?.ReleaseMutex(); - _appMutex?.Dispose(); + _mutex?.ReleaseMutex(); + _mutex?.Dispose(); } } @@ -428,103 +426,14 @@ private enum VersionCheckResult Restart } - private static void HandleExistingInstance() + private static void ShowRunningInstanceWarning() { - var existingProcesses = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName) - .Where(p => p.Id != Process.GetCurrentProcess().Id) - .ToList(); - - if (existingProcesses.Count == 0) return; - - var existingVersion = GetRunningVersion(existingProcesses[0]); - int versionComparison = existingVersion.CompareTo(CurrentVersion); - - if (versionComparison == 0) // Same version - { - MessageBox.Show($"{AppName} is already running.\n\n" + - "Please check your system tray or task manager.", - "Information", - MessageBoxButtons.OK, - MessageBoxIcon.Information); - } - else if (versionComparison > 0) // Newer version running - { - MessageBox.Show($"A newer version (v{existingVersion}) is already running!\n\n" + - $"Please close this version (v{CurrentVersion}) and use the newer one.", - "New Version Detected", - MessageBoxButtons.OK, - MessageBoxIcon.Information); - } - else // Older version running - { - var result = MessageBox.Show( - $"An older version (v{existingVersion}) is running.\n\n" + - $"Current version: v{CurrentVersion}\n\n" + - "Would you like to upgrade now?", - "Upgrade Available", - MessageBoxButtons.YesNo, - MessageBoxIcon.Question); - - if (result == DialogResult.Yes) - { - try - { - var process = existingProcesses[0]; - // Try graceful shutdown first - if (!process.CloseMainWindow()) - { - process.Kill(); - } - - if (!process.WaitForExit(3000)) - { - MessageBox.Show( - "Could not close the previous version.\n" + - "Please close it manually and run the new version again.", - "Upgrade Warning", - MessageBoxButtons.OK, - MessageBoxIcon.Warning); - return; - } - - Thread.Sleep(500); // Brief pause before restart - Process.Start(new ProcessStartInfo - { - FileName = Application.ExecutablePath, - UseShellExecute = true - }); - } - catch (Exception ex) - { - MessageBox.Show( - $"Upgrade failed: {ex.Message}\n\n" + - "Please close the old version manually and try again.", - "Upgrade Error", - MessageBoxButtons.OK, - MessageBoxIcon.Error); - } - } - } - } - - private static Version GetRunningVersion(Process process) - { - try - { - if (process.MainModule?.FileName is string fileName) - { - var versionInfo = FileVersionInfo.GetVersionInfo(fileName); - if (versionInfo.FileVersion != null) - { - return new Version(versionInfo.FileVersion); - } - } - } - catch - { - // Fallback if version can't be determined - } - return new Version(0, 0); + MessageBox.Show( + $"{AppName} is already running.\n\n" + + "Please check your system tray or task manager.", + "Application Running", + MessageBoxButtons.OK, + MessageBoxIcon.Information); } private static VersionCheckResult CheckRunningVersions() @@ -545,6 +454,12 @@ private static VersionCheckResult CheckRunningVersions() if (comparison > 0) // Newer version running { + MessageBox.Show( + $"A newer version (v{runningVersion}) is already running!\n\n" + + $"Please close this version (v{currentVersion}) and use the newer one.", + "New Version Detected", + MessageBoxButtons.OK, + MessageBoxIcon.Information); return VersionCheckResult.Exit; } else if (comparison < 0) // Older version running From 96b8cc65b9afb23ccaba7abb7f57dd227c258473 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 03:11:21 +0800 Subject: [PATCH 14/18] Update Program.cs --- Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index 825f32e..0eb3fef 100644 --- a/Program.cs +++ b/Program.cs @@ -430,7 +430,7 @@ private static void ShowRunningInstanceWarning() { MessageBox.Show( $"{AppName} is already running.\n\n" + - "Please check your system tray or task manager.", + "Please exit old instance and rerun new version to upgrade.", "Application Running", MessageBoxButtons.OK, MessageBoxIcon.Information); From b727a691231b241795319dee011c57d50f91506f Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 03:18:22 +0800 Subject: [PATCH 15/18] Update Program.cs --- Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index 0eb3fef..7b759d4 100644 --- a/Program.cs +++ b/Program.cs @@ -430,7 +430,8 @@ private static void ShowRunningInstanceWarning() { MessageBox.Show( $"{AppName} is already running.\n\n" + - "Please exit old instance and rerun new version to upgrade.", + "If you have new version or move exe file to new location", + "Please exit old instance and rerun new version to update.", "Application Running", MessageBoxButtons.OK, MessageBoxIcon.Information); From 0bddc7c4cfe91ef2505e296004703f2caa78aa19 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 03:22:47 +0800 Subject: [PATCH 16/18] Update Program.cs --- Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Program.cs b/Program.cs index 7b759d4..da0068e 100644 --- a/Program.cs +++ b/Program.cs @@ -430,8 +430,8 @@ private static void ShowRunningInstanceWarning() { MessageBox.Show( $"{AppName} is already running.\n\n" + - "If you have new version or move exe file to new location", - "Please exit old instance and rerun new version to update.", + "If you have new version or move exe file to new location" + + "Please exit old instance and rerun new version to update.", "Application Running", MessageBoxButtons.OK, MessageBoxIcon.Information); From 04a69fd00c8cb86e5ea8dc9f3fed2730a9e4e743 Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 03:27:25 +0800 Subject: [PATCH 17/18] Update Program.cs --- Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index da0068e..68efc17 100644 --- a/Program.cs +++ b/Program.cs @@ -430,7 +430,7 @@ private static void ShowRunningInstanceWarning() { MessageBox.Show( $"{AppName} is already running.\n\n" + - "If you have new version or move exe file to new location" + + "If updating version or exe file location.\n" + "Please exit old instance and rerun new version to update.", "Application Running", MessageBoxButtons.OK, From 121215b27a06a74edbdf26a4a7c17c65168c4c1d Mon Sep 17 00:00:00 2001 From: monkey8113 Date: Sat, 2 Aug 2025 03:30:44 +0800 Subject: [PATCH 18/18] Update SpotifyPreventLock.csproj --- SpotifyPreventLock.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpotifyPreventLock.csproj b/SpotifyPreventLock.csproj index b7236c8..ba9458c 100644 --- a/SpotifyPreventLock.csproj +++ b/SpotifyPreventLock.csproj @@ -8,7 +8,7 @@ true - 1.1.2 + 1.1.1 dev