mirror of
https://github.com/Stone-Red-Code/DesktopMagic.git
synced 2026-09-05 07:46:15 +02:00
Add check so that only one instance can be open at a time
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user