Add check so that only one instance can be open at a time

This commit is contained in:
Stone-Red-Code
2021-08-25 19:08:26 +02:00
parent ae490367ee
commit 8df53ef190
2 changed files with 60 additions and 39 deletions
+28 -1
View File
@@ -1,4 +1,6 @@
using System.Windows;
using System;
using System.Threading;
using System.Windows;
namespace DesktopMagic
{
@@ -7,5 +9,30 @@ namespace DesktopMagic
/// </summary>
public partial class App : Application
{
private Mutex _mutex;
public App()
{
// Try to grab mutex
bool createdNew;
_mutex = new Mutex(true, $"Stone_Red{DesktopMagic.MainWindow.AppName}", out createdNew);
//check if creating new was succesfull
if (!createdNew)
{
//Shutdown Aplication
Current.Shutdown();
}
else
{
// Add Event handler to exit event.
Exit += CloseMutexHandler;
}
}
protected virtual void CloseMutexHandler(object sender, EventArgs e)
{
_mutex?.Close();
}
}
}