mirror of
https://github.com/bloxstraplabs/bloxstrap.git
synced 2025-04-21 10:01:27 -07:00
use mutexes
This commit is contained in:
parent
b5114c8e73
commit
635d4492aa
@ -163,19 +163,22 @@ namespace Bloxstrap
|
|||||||
InstallChecker.CheckUpgrade();
|
InstallChecker.CheckUpgrade();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
using InterProcessLock processLock = new InterProcessLock("Process", TimeSpan.FromMilliseconds(100));
|
||||||
|
|
||||||
if (LaunchSettings.IsMenuLaunch)
|
if (LaunchSettings.IsMenuLaunch)
|
||||||
{
|
{
|
||||||
Process? menuProcess = Process.GetProcesses().Where(x => x.MainWindowTitle == $"{ProjectName} Menu").FirstOrDefault();
|
using InterProcessLock menuLock = new InterProcessLock("Menu", TimeSpan.FromMilliseconds(100));
|
||||||
|
|
||||||
if (menuProcess is not null)
|
if (!menuLock.IsAcquired)
|
||||||
{
|
{
|
||||||
var handle = menuProcess.MainWindowHandle;
|
Frontend.ShowMessageBox(
|
||||||
Logger.WriteLine(LOG_IDENT, $"Found an already existing menu window with handle {handle}");
|
"The Bloxstrap menu is already open.",
|
||||||
PInvoke.SetForegroundWindow((HWND)handle);
|
MessageBoxImage.Error
|
||||||
|
);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (Process.GetProcessesByName(ProjectName).Length > 1 && !LaunchSettings.IsQuiet)
|
if (!processLock.IsAcquired && !LaunchSettings.IsQuiet)
|
||||||
Frontend.ShowMessageBox(
|
Frontend.ShowMessageBox(
|
||||||
Bloxstrap.Resources.Strings.Menu_AlreadyRunning,
|
Bloxstrap.Resources.Strings.Menu_AlreadyRunning,
|
||||||
MessageBoxImage.Information
|
MessageBoxImage.Information
|
||||||
|
30
Bloxstrap/InterProcessLock.cs
Normal file
30
Bloxstrap/InterProcessLock.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
SetForegroundWindow
|
FlashWindow
|
||||||
FlashWindow
|
|
||||||
GetWindowLong
|
GetWindowLong
|
||||||
SetWindowLong
|
SetWindowLong
|
||||||
EnumDisplaySettings
|
EnumDisplaySettings
|
||||||
|
Loading…
Reference in New Issue
Block a user