From d9f8cace4324fa0be553cfbaa24c552956855781 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Fri, 16 Aug 2024 20:45:37 +0100 Subject: [PATCH] Add mutexes for settings, installer and uninstaller --- Bloxstrap/LaunchHandler.cs | 48 ++++++++++++++++--------- Bloxstrap/Resources/Strings.Designer.cs | 18 ++++++++++ Bloxstrap/Resources/Strings.resx | 6 ++++ Bloxstrap/Utility/InterProcessLock.cs | 4 +++ 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/Bloxstrap/LaunchHandler.cs b/Bloxstrap/LaunchHandler.cs index cc40ef1..a659d8b 100644 --- a/Bloxstrap/LaunchHandler.cs +++ b/Bloxstrap/LaunchHandler.cs @@ -1,12 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; +using System.Windows; using Bloxstrap.UI.Elements.Dialogs; -using Bloxstrap.Resources; using Microsoft.Win32; using Windows.Win32; @@ -50,7 +44,13 @@ namespace Bloxstrap public static void LaunchInstaller() { - // TODO: detect duplicate launch, mutex maybe? + using var interlock = new InterProcessLock("Installer"); + + if (!interlock.IsAcquired) + { + Frontend.ShowMessageBox(Strings.Dialog_AlreadyRunning_Installer, MessageBoxImage.Stop); + return; + } if (App.LaunchSettings.IsUninstall) { @@ -68,6 +68,8 @@ namespace Bloxstrap installer.DoInstall(); + interlock.Dispose(); + ProcessLaunchArgs(); } else @@ -77,6 +79,8 @@ namespace Bloxstrap var installer = new UI.Elements.Installer.MainWindow(); installer.ShowDialog(); + interlock.Dispose(); + ProcessNextAction(installer.CloseAction, !installer.Finished); } @@ -84,6 +88,15 @@ namespace Bloxstrap public static void LaunchUninstaller() { + using var interlock = new InterProcessLock("Uninstaller"); + + if (!interlock.IsAcquired) + { + Frontend.ShowMessageBox(Strings.Dialog_AlreadyRunning_Uninstaller, MessageBoxImage.Stop); + return; + } + + bool confirmed = false; bool keepData = true; @@ -112,20 +125,21 @@ namespace Bloxstrap { const string LOG_IDENT = "LaunchHandler::LaunchSettings"; - // TODO: move to mutex (especially because multi language whatever) + using var interlock = new InterProcessLock("Settings"); - Process? menuProcess = Utilities.GetProcessesSafe().Where(x => x.MainWindowTitle == Strings.Menu_Title).FirstOrDefault(); - - if (menuProcess is not null) + if (interlock.IsAcquired) { - var handle = menuProcess.MainWindowHandle; - App.Logger.WriteLine(LOG_IDENT, $"Found an already existing menu window with handle {handle}"); - PInvoke.SetForegroundWindow((HWND)handle); + bool showAlreadyRunningWarning = Process.GetProcessesByName(App.ProjectName).Length > 1; + new UI.Elements.Settings.MainWindow(showAlreadyRunningWarning).ShowDialog(); } else { - bool showAlreadyRunningWarning = Process.GetProcessesByName(App.ProjectName).Length > 1 && !App.LaunchSettings.IsQuiet; - new UI.Elements.Settings.MainWindow(showAlreadyRunningWarning).ShowDialog(); + App.Logger.WriteLine(LOG_IDENT, $"Found an already existing menu window"); + + var process = Utilities.GetProcessesSafe().Where(x => x.MainWindowTitle == Strings.Menu_Title).FirstOrDefault(); + + if (process is not null) + PInvoke.SetForegroundWindow((HWND)process.MainWindowHandle); } } diff --git a/Bloxstrap/Resources/Strings.Designer.cs b/Bloxstrap/Resources/Strings.Designer.cs index 1292ce4..10e9947 100644 --- a/Bloxstrap/Resources/Strings.Designer.cs +++ b/Bloxstrap/Resources/Strings.Designer.cs @@ -729,6 +729,24 @@ namespace Bloxstrap.Resources { } } + /// + /// Looks up a localized string similar to Please wait for installation to finish.. + /// + public static string Dialog_AlreadyRunning_Installer { + get { + return ResourceManager.GetString("Dialog.AlreadyRunning.Installer", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Please wait for uninstallation to finish.. + /// + public static string Dialog_AlreadyRunning_Uninstaller { + get { + return ResourceManager.GetString("Dialog.AlreadyRunning.Uninstaller", resourceCulture); + } + } + /// /// Looks up a localized string similar to Bloxstrap was unable to create shortcuts for the Desktop and Start menu. Try creating them later through the settings.. /// diff --git a/Bloxstrap/Resources/Strings.resx b/Bloxstrap/Resources/Strings.resx index a200efc..0fb0885 100644 --- a/Bloxstrap/Resources/Strings.resx +++ b/Bloxstrap/Resources/Strings.resx @@ -1124,4 +1124,10 @@ If not, then please report this exception to the maintainers of this fork. Do NO {0} + + Please wait for installation to finish. + + + Please wait for uninstallation to finish. + \ No newline at end of file diff --git a/Bloxstrap/Utility/InterProcessLock.cs b/Bloxstrap/Utility/InterProcessLock.cs index 2a080e8..eee06a2 100644 --- a/Bloxstrap/Utility/InterProcessLock.cs +++ b/Bloxstrap/Utility/InterProcessLock.cs @@ -12,6 +12,8 @@ namespace Bloxstrap.Utility public bool IsAcquired { get; private set; } + public InterProcessLock(string name) : this(name, TimeSpan.Zero) { } + public InterProcessLock(string name, TimeSpan timeout) { Mutex = new Mutex(false, "Bloxstrap-" + name); @@ -25,6 +27,8 @@ namespace Bloxstrap.Utility Mutex.ReleaseMutex(); IsAcquired = false; } + + GC.SuppressFinalize(this); } } }