diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index 9e39d45..a16478d 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -228,24 +228,21 @@ namespace Bloxstrap if (IsMenuLaunch) { - Mutex mutex; + Process? menuProcess = Process.GetProcesses().Where(x => x.MainWindowTitle == $"{ProjectName} Menu").FirstOrDefault(); - try + if (menuProcess is not null) { - mutex = Mutex.OpenExisting("Bloxstrap_MenuMutex"); - Logger.WriteLine("[App::OnStartup] Bloxstrap_MenuMutex mutex exists, aborting menu launch..."); - Terminate(); + IntPtr handle = menuProcess.MainWindowHandle; + Logger.WriteLine($"[App::OnStartup] Found an already existing menu window with handle {handle}"); + NativeMethods.SetForegroundWindow(handle); } - catch + else { - // no mutex exists, continue to opening preferences menu - mutex = new(true, "Bloxstrap_MenuMutex"); + if (Process.GetProcessesByName(ProjectName).Length > 1) + ShowMessageBox($"{ProjectName} is currently running, likely as a background Roblox process. Please note that not all your changes will immediately apply until you close all currently open Roblox instances.", MessageBoxImage.Information); + + new MainWindow().ShowDialog(); } - - if (Utilities.GetProcessCount(ProjectName) > 1) - ShowMessageBox($"{ProjectName} is currently running, likely as a background Roblox process. Please note that not all your changes will immediately apply until you close all currently open Roblox instances.", MessageBoxImage.Information); - - new MainWindow().ShowDialog(); } else if (LaunchArgs.Length > 0) { diff --git a/Bloxstrap/NativeMethods.cs b/Bloxstrap/NativeMethods.cs new file mode 100644 index 0000000..7cf6981 --- /dev/null +++ b/Bloxstrap/NativeMethods.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace Bloxstrap +{ + static class NativeMethods + { + [DllImport("user32.dll")] + public static extern bool SetForegroundWindow(IntPtr hWnd); + } +}