Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat(boot): prevent multiple application instances

Lars Bergerd6681ccb842b
diff --git a/GlazeWM.Bootstrapper/Program.cs b/GlazeWM.Bootstrapper/Program.cs
index 3e302611..7edce4e8 100644
--- a/GlazeWM.Bootstrapper/Program.cs
+++ b/GlazeWM.Bootstrapper/Program.cs
@@ -4,33 +4,50 @@ using GlazeWM.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Diagnostics;
+using System.Threading;
+using System.Windows.Forms;
using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;
namespace GlazeWM.Bootstrapper
{
static class Program
{
+ private static string appGuid = "325d0ed7-7f60-4925-8d1b-aa287b26b218";
+
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
- Debug.WriteLine("Application started");
+ Debug.WriteLine("Application started.");
+
+ // Prevent multiple app instances using a global UUID mutex.
+ using (Mutex mutex = new Mutex(false, "Global\\" + appGuid))
+ {
+ if (!mutex.WaitOne(0, false))
+ {
+ Debug.Write(
+ "Application is already running. Only one instance of this application is allowed."
+ );
+ return;
+ }
- // Set the process-default DPI awareness.
- SetProcessDpiAwarenessContext(DpiAwarenessContext.Context_PerMonitorAwareV2);
+ // Set the process-default DPI awareness.
+ SetProcessDpiAwarenessContext(DpiAwarenessContext.Context_PerMonitorAwareV2);
- var serviceCollection = new ServiceCollection();
- serviceCollection.AddInfrastructureServices();
- serviceCollection.AddDomainServices();
- serviceCollection.AddBarServices();
- serviceCollection.AddSingleton<Startup>();
+ var serviceCollection = new ServiceCollection();
+ serviceCollection.AddInfrastructureServices();
+ serviceCollection.AddDomainServices();
+ serviceCollection.AddBarServices();
+ serviceCollection.AddSingleton<Startup>();
- ServiceLocator.Provider = serviceCollection.BuildServiceProvider();
+ ServiceLocator.Provider = serviceCollection.BuildServiceProvider();
- var startup = ServiceLocator.Provider.GetRequiredService<Startup>();
- startup.Init();
+ var startup = ServiceLocator.Provider.GetRequiredService<Startup>();
+ startup.Init();
+ Application.Run();
+ }
}
}
}