Skip to content

Commit 5a4cbab

Browse files
committed
Add single instance enforcement to prevent multiple app instances
- Uses named Mutex to ensure only one instance can run at a time - Shows friendly message if user tries to launch a second instance - Properly releases mutex on application exit - Prevents accidental double-launches and resource conflicts
1 parent 0ffa9e0 commit 5a4cbab

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

WindowsEdgeLight/App.xaml.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace WindowsEdgeLight;
1111
/// </summary>
1212
public partial class App : System.Windows.Application
1313
{
14+
private static System.Threading.Mutex? _mutex;
15+
1416
internal static readonly UpdatumManager AppUpdater = new("shanselman", "WindowsEdgeLight")
1517
{
1618
// Default pattern (win-x64) will match our ZIP assets
@@ -22,12 +24,34 @@ public partial class App : System.Windows.Application
2224

2325
protected override async void OnStartup(StartupEventArgs e)
2426
{
27+
// Check for single instance using a named mutex
28+
const string mutexName = "WindowsEdgeLight_SingleInstance_Mutex";
29+
bool createdNew;
30+
31+
_mutex = new System.Threading.Mutex(true, mutexName, out createdNew);
32+
33+
if (!createdNew)
34+
{
35+
// Another instance is already running
36+
MessageBox.Show("Windows Edge Light is already running.\n\nCheck your system tray for the application icon.",
37+
"Already Running", MessageBoxButton.OK, MessageBoxImage.Information);
38+
Shutdown();
39+
return;
40+
}
41+
2542
base.OnStartup(e);
2643

2744
// Check for updates asynchronously
2845
_ = CheckForUpdatesAsync();
2946
}
3047

48+
protected override void OnExit(ExitEventArgs e)
49+
{
50+
_mutex?.ReleaseMutex();
51+
_mutex?.Dispose();
52+
base.OnExit(e);
53+
}
54+
3155
private async Task CheckForUpdatesAsync()
3256
{
3357
try

0 commit comments

Comments
 (0)