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> /// </summary>
public partial class App : Application public partial class App : Application
{ {
public const string AppGuid = "{{61FE5CE9-47C3-4255-A1F4-5BCF4ACA0879}";
public const string AppName = "Desktop Magic"; public const string AppName = "Desktop Magic";
private readonly string logFilePath; private readonly string logFilePath;
private readonly Mutex _mutex;
#if DEBUG #if DEBUG
private readonly Updater updater = new Updater(TimeSpan.FromDays(1), "https://raw.githubusercontent.com/Stone-Red-Code/DesktopMagic/develop/update/updateInfo.json"); private readonly Updater updater = new Updater(TimeSpan.FromDays(1), "https://raw.githubusercontent.com/Stone-Red-Code/DesktopMagic/develop/update/updateInfo.json");
#else #else
private readonly Updater updater = new Updater(TimeSpan.FromDays(1), "https://raw.githubusercontent.com/Stone-Red-Code/DesktopMagic/main/update/updateInfo.json"); private readonly Updater updater = new Updater(TimeSpan.FromDays(1), "https://raw.githubusercontent.com/Stone-Red-Code/DesktopMagic/main/update/updateInfo.json");
#endif #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 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() public App()
{ {
logFilePath = ApplicationDataPath + "\\Log.log"; logFilePath = ApplicationDataPath + "\\Log.log";
// Try to grab mutex // Setup global event handler
_mutex = new Mutex(true, $"Stone_Red{AppName}", out bool createdNew); eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, AppGuid, out bool createdNew);
//check if creating new was successful //check if creating new was successful
if (!createdNew) if (!createdNew)
{ {
Setup(false); Setup(false);
Logger.Log("Shutting down because other instance already running.", "Setup"); Logger.Log("Shutting down because other instance already running.", "Setup", LogSeverity.Warn);
_ = MessageBox.Show($"Another instance of {AppName} is already running.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
//Shutdown Application //Shutdown Application
_ = eventWaitHandle.Set();
Current.Shutdown(); Current.Shutdown();
} }
else else
{ {
eventThread = new Thread(
() =>
{
while (eventWaitHandle.WaitOne())
{
_ = Current.Dispatcher.BeginInvoke(
() => ((MainWindow)Current.MainWindow).RestoreWindow());
}
});
eventThread.Start();
Setup(true); Setup(true);
Exit += CloseMutexHandler; Exit += CloseHandler;
updater.ProgressChanged += Updater_ProgressChanged; updater.ProgressChanged += Updater_ProgressChanged;
updater.OnException += Updater_OnException; 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) private void Updater_UpdateAvailible(string version, string additionalInformation)
+14 -9
View File
@@ -356,6 +356,19 @@ namespace DesktopMagic
#endregion Options #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) private void TextBlock_Loaded(object sender, RoutedEventArgs e)
{ {
int index = 0; int index = 0;
@@ -600,15 +613,7 @@ namespace DesktopMagic
private void TaskbarIcon_TrayLeftClick(object? sender, EventArgs e) private void TaskbarIcon_TrayLeftClick(object? sender, EventArgs e)
{ {
for (int i = 0; i < 10; i++) RestoreWindow();
{
ShowInTaskbar = true;
Visibility = Visibility.Visible;
SystemCommands.RestoreWindow(this);
Topmost = true;
_ = Activate();
Topmost = false;
}
} }
private void GithubButton_Click(object sender, RoutedEventArgs e) private void GithubButton_Click(object sender, RoutedEventArgs e)