From d3d134ae0d769cf42d0ad5ff1dccfef4b4deaed9 Mon Sep 17 00:00:00 2001 From: bluepilledgreat <97983689+bluepilledgreat@users.noreply.github.com> Date: Wed, 7 Feb 2024 15:00:38 +0000 Subject: [PATCH] Revert "use mutexes" This reverts commit 635d4492aa590b93939e0311c083ad184cfa4a6d. --- Bloxstrap/App.xaml.cs | 15 ++++++--------- Bloxstrap/InterProcessLock.cs | 30 ------------------------------ Bloxstrap/NativeMethods.txt | 3 ++- 3 files changed, 8 insertions(+), 40 deletions(-) delete mode 100644 Bloxstrap/InterProcessLock.cs diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index ad3b761..f6e7072 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -163,22 +163,19 @@ namespace Bloxstrap InstallChecker.CheckUpgrade(); #endif - using InterProcessLock processLock = new InterProcessLock("Process", TimeSpan.FromMilliseconds(100)); - if (LaunchSettings.IsMenuLaunch) { - using InterProcessLock menuLock = new InterProcessLock("Menu", TimeSpan.FromMilliseconds(100)); + Process? menuProcess = Process.GetProcesses().Where(x => x.MainWindowTitle == $"{ProjectName} Menu").FirstOrDefault(); - if (!menuLock.IsAcquired) + if (menuProcess is not null) { - Frontend.ShowMessageBox( - "The Bloxstrap menu is already open.", - MessageBoxImage.Error - ); + var handle = menuProcess.MainWindowHandle; + Logger.WriteLine(LOG_IDENT, $"Found an already existing menu window with handle {handle}"); + PInvoke.SetForegroundWindow((HWND)handle); } else { - if (!processLock.IsAcquired && !LaunchSettings.IsQuiet) + if (Process.GetProcessesByName(ProjectName).Length > 1 && !LaunchSettings.IsQuiet) Frontend.ShowMessageBox( Bloxstrap.Resources.Strings.Menu_AlreadyRunning, MessageBoxImage.Information diff --git a/Bloxstrap/InterProcessLock.cs b/Bloxstrap/InterProcessLock.cs deleted file mode 100644 index e1938e8..0000000 --- a/Bloxstrap/InterProcessLock.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Bloxstrap -{ - public class InterProcessLock : IDisposable - { - public Mutex Mutex { get; private set; } - - public bool IsAcquired { get; private set; } - - public InterProcessLock(string name, TimeSpan timeout) - { - Mutex = new Mutex(false, "Bloxstrap-" + name); - IsAcquired = Mutex.WaitOne(timeout); - } - - public void Dispose() - { - if (IsAcquired) - { - Mutex.ReleaseMutex(); - IsAcquired = false; - } - } - } -} diff --git a/Bloxstrap/NativeMethods.txt b/Bloxstrap/NativeMethods.txt index bc49657..aaa0b6a 100644 --- a/Bloxstrap/NativeMethods.txt +++ b/Bloxstrap/NativeMethods.txt @@ -1,4 +1,5 @@ -FlashWindow +SetForegroundWindow +FlashWindow GetWindowLong SetWindowLong EnumDisplaySettings