Improve how multiple menu instances are handled

This commit is contained in:
pizzaboxer 2023-06-12 12:16:42 +01:00
parent 778a67dcb9
commit 3926f30866
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
2 changed files with 25 additions and 13 deletions

View File

@ -228,24 +228,21 @@ namespace Bloxstrap
if (IsMenuLaunch) 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"); IntPtr handle = menuProcess.MainWindowHandle;
Logger.WriteLine("[App::OnStartup] Bloxstrap_MenuMutex mutex exists, aborting menu launch..."); Logger.WriteLine($"[App::OnStartup] Found an already existing menu window with handle {handle}");
Terminate(); NativeMethods.SetForegroundWindow(handle);
} }
catch else
{ {
// no mutex exists, continue to opening preferences menu if (Process.GetProcessesByName(ProjectName).Length > 1)
mutex = new(true, "Bloxstrap_MenuMutex"); 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) else if (LaunchArgs.Length > 0)
{ {

View File

@ -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);
}
}