Fix mutex and open app if it's already running

This commit is contained in:
Stone_Red
2023-11-22 16:01:45 +01:00
parent e8de6fe9a5
commit 69672da48f
2 changed files with 49 additions and 18 deletions
+35 -9
View File
@@ -14,38 +14,63 @@ namespace DesktopMagic;
/// </summary>
public partial class App : Application
{
public const string AppGuid = "{{61FE5CE9-47C3-4255-A1F4-5BCF4ACA0879}";
public const string AppName = "Desktop Magic";
private readonly string logFilePath;
private readonly Mutex _mutex;
#if DEBUG
private readonly Updater updater = new Updater(TimeSpan.FromDays(1), "https://raw.githubusercontent.com/Stone-Red-Code/DesktopMagic/develop/update/updateInfo.json");
#else
private readonly Updater updater = new Updater(TimeSpan.FromDays(1), "https://raw.githubusercontent.com/Stone-Red-Code/DesktopMagic/main/update/updateInfo.json");
#endif
private Thread? eventThread;
private EventWaitHandle eventWaitHandle;
private Logger logger = new Logger();
public static string ApplicationDataPath { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "StoneRed", AppName);
public static Logger Logger { get; } = new Logger();
public static Logger Logger
{
get
{
lock (Current)
{
return ((App)Current).logger;
}
}
}
public App()
{
logFilePath = ApplicationDataPath + "\\Log.log";
// Try to grab mutex
_mutex = new Mutex(true, $"Stone_Red{AppName}", out bool createdNew);
// Setup global event handler
eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, AppGuid, out bool createdNew);
//check if creating new was successful
if (!createdNew)
{
Setup(false);
Logger.Log("Shutting down because other instance already running.", "Setup");
_ = MessageBox.Show($"Another instance of {AppName} is already running.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Logger.Log("Shutting down because other instance already running.", "Setup", LogSeverity.Warn);
//Shutdown Application
_ = eventWaitHandle.Set();
Current.Shutdown();
}
else
{
eventThread = new Thread(
() =>
{
while (eventWaitHandle.WaitOne())
{
_ = Current.Dispatcher.BeginInvoke(
() => ((MainWindow)Current.MainWindow).RestoreWindow());
}
});
eventThread.Start();
Setup(true);
Exit += CloseMutexHandler;
Exit += CloseHandler;
updater.ProgressChanged += Updater_ProgressChanged;
updater.OnException += Updater_OnException;
@@ -55,9 +80,10 @@ public partial class App : Application
}
}
protected void CloseMutexHandler(object sender, EventArgs e)
protected void CloseHandler(object sender, EventArgs e)
{
_mutex?.Close();
eventWaitHandle.Close();
eventThread?.Interrupt();
}
private void Updater_UpdateAvailible(string version, string additionalInformation)
+14 -9
View File
@@ -356,6 +356,19 @@ namespace DesktopMagic
#endregion Options
internal void RestoreWindow()
{
for (int i = 0; i < 10; i++)
{
ShowInTaskbar = true;
Visibility = Visibility.Visible;
SystemCommands.RestoreWindow(this);
Topmost = true;
_ = Activate();
Topmost = false;
}
}
private void TextBlock_Loaded(object sender, RoutedEventArgs e)
{
int index = 0;
@@ -600,15 +613,7 @@ namespace DesktopMagic
private void TaskbarIcon_TrayLeftClick(object? sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
ShowInTaskbar = true;
Visibility = Visibility.Visible;
SystemCommands.RestoreWindow(this);
Topmost = true;
_ = Activate();
Topmost = false;
}
RestoreWindow();
}
private void GithubButton_Click(object sender, RoutedEventArgs e)