Use guid instead of app name to create the mutex

This commit is contained in:
Stone_Red
2023-11-22 16:53:31 +01:00
parent 64c693e81b
commit 923b00cfc8
2 changed files with 24 additions and 8 deletions
+16 -7
View File
@@ -1,4 +1,5 @@
using System.Threading;
using System;
using System.Threading;
using System.Windows;
namespace ClipCMD;
@@ -8,20 +9,28 @@ namespace ClipCMD;
/// </summary>
public partial class App : Application
{
Mutex mutex;
public const string AppName = "ClipCMD";
public const string AppGuid = "68d780d4-b946-4154-8815-e4632e5ec5d8";
private Mutex? mutex;
protected override void OnStartup(StartupEventArgs e)
{
const string appName = "ClipCMD";
mutex = new Mutex(true, appName, out bool createdNew);
mutex = new Mutex(true, AppGuid, out bool createdNew);
if (!createdNew)
{
//App is already running! Exiting the application
_ = MessageBox.Show($"Another instance of {appName} already running!", $"{appName} is already running!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
_ = MessageBox.Show($"Another instance of {AppName} already running!", $"{AppName} is already running!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
Current.Shutdown();
}
base.OnStartup(e);
Exit += CloseHandler;
}
protected void CloseHandler(object sender, EventArgs e)
{
mutex?.ReleaseMutex();
}
}
+8 -1
View File
@@ -8,6 +8,7 @@ using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
@@ -45,7 +46,7 @@ public partial class MainWindow : Window
notifyIcon.Click += NotifyIcon_Click;
string applicationDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string applicationDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "StoneRed");
string folderPath = Path.Combine(applicationDataPath, "ClipCMD");
if (!Directory.Exists(folderPath))
@@ -69,6 +70,12 @@ public partial class MainWindow : Window
DataContext = this;
InitializeComponent();
#if DEBUG
Title = $"{App.AppName} - Dev {Assembly.GetExecutingAssembly().GetName().Version}";
#else
Title = $"{App.AppName} - {Assembly.GetExecutingAssembly().GetName().Version}";
#endif
}
protected override void OnSourceInitialized(EventArgs e)