Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Dashboard/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ public partial class App : Application
{
private const string MutexName = "PerformanceMonitorDashboard_SingleInstance";
private Mutex? _singleInstanceMutex;
private bool _ownsMutex;

protected override void OnStartup(StartupEventArgs e)
{
// Check for existing instance
_singleInstanceMutex = new Mutex(true, MutexName, out bool isNewInstance);
_singleInstanceMutex = new Mutex(true, MutexName, out _ownsMutex);

if (!isNewInstance)
if (!_ownsMutex)
{
// Another instance is already running - activate it and exit
NativeMethods.BroadcastShowMessage();
Expand Down Expand Up @@ -58,8 +59,10 @@ protected override void OnExit(ExitEventArgs e)
mainWin.ExitApplication();
}

// Release the mutex
_singleInstanceMutex?.ReleaseMutex();
if (_ownsMutex)
{
_singleInstanceMutex?.ReleaseMutex();
}
_singleInstanceMutex?.Dispose();

base.OnExit(e);
Expand Down
11 changes: 7 additions & 4 deletions Lite/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class App : Application
{
private const string MutexName = "PerformanceMonitorLite_SingleInstance";
private Mutex? _singleInstanceMutex;
private bool _ownsMutex;

/// <summary>
/// Gets the application data directory where config and data files are stored.
Expand Down Expand Up @@ -119,9 +120,9 @@ protected override void OnStartup(StartupEventArgs e)
{

// Check for existing instance
_singleInstanceMutex = new Mutex(true, MutexName, out bool isNewInstance);
_singleInstanceMutex = new Mutex(true, MutexName, out _ownsMutex);

if (!isNewInstance)
if (!_ownsMutex)
{
MessageBox.Show(
"Performance Monitor Lite is already running.",
Expand Down Expand Up @@ -168,8 +169,10 @@ protected override void OnExit(ExitEventArgs e)
AppLogger.Info("App", "Shutting down");
AppLogger.Shutdown();

// Release the mutex
_singleInstanceMutex?.ReleaseMutex();
if (_ownsMutex)
{
_singleInstanceMutex?.ReleaseMutex();
}
_singleInstanceMutex?.Dispose();

base.OnExit(e);
Expand Down
Loading