fix: 尝试修复版本更新后因互斥锁遇到的问题

This commit is contained in:
uye
2023-05-14 02:46:19 +08:00
parent b5172a58ef
commit efe3f25843
4 changed files with 41 additions and 40 deletions

View File

@@ -33,40 +33,5 @@ namespace MaaWpfGui
Process.Start(new ProcessStartInfo(link.NavigateUri.AbsoluteUri));
}
}
//private Mutex _mutex;
protected override void OnStartup(StartupEventArgs e)
{
// FIXME: 版本更新之后,这里重新打开进程应用更新包,这里是必炸的
// 先全删掉,等学姐来修(
//// 设置互斥量的名称
//string mutexName = "MAA_" + Directory.GetCurrentDirectory().Replace("\\", "_").Replace(":", string.Empty);
//_mutex = new Mutex(true, mutexName);
//if (!_mutex.WaitOne(TimeSpan.Zero, true))
//{
// // 这里还没加载语言包,就不 GetString 了
// MessageBox.Show("同一路径下只能启动一个实例!\n\n" +
// "同一路徑下只能啟動一個實例!\n\n" +
// "Only one instance can be launched under the same path!\n\n" +
// "同じパスの下で1つのインスタンスしか起動できません\n\n" +
// "동일한 경로에는 하나의 인스턴스만 실행할 수 있습니다!");
// Current.Shutdown();
// return;
//}
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
//// 释放互斥量
//_mutex.ReleaseMutex();
//_mutex.Dispose();
base.OnExit(e);
}
}
}

View File

@@ -14,6 +14,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using GlobalHotKey;
@@ -38,11 +39,28 @@ namespace MaaWpfGui.Main
public class Bootstrapper : Bootstrapper<RootViewModel>
{
private static ILogger _logger = Logger.None;
private static Mutex _mutex;
/// <inheritdoc/>
/// <remarks>初始化些啥自己加。</remarks>
protected override void OnStart()
{
// 设置互斥量的名称
string mutexName = "MAA_" + Directory.GetCurrentDirectory().Replace("\\", "_").Replace(":", string.Empty);
_mutex = new Mutex(true, mutexName);
if (!_mutex.WaitOne(TimeSpan.Zero, true))
{
// 这里还没加载语言包,就不 GetString 了
MessageBox.Show("同一路径下只能启动一个实例!\n\n" +
"同一路徑下只能啟動一個實例!\n\n" +
"Only one instance can be launched under the same path!\n\n" +
"同じパスの下で1つのインスタンスしか起動できません\n\n" +
"동일한 경로에는 하나의 인스턴스만 실행할 수 있습니다!");
Application.Current.Shutdown();
return;
}
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
if (Directory.Exists("debug") is false)
{
@@ -158,6 +176,10 @@ namespace MaaWpfGui.Main
/// <remarks>退出时执行啥自己加。</remarks>
protected override void OnExit(ExitEventArgs e)
{
// 释放互斥量
_mutex?.ReleaseMutex();
_mutex?.Dispose();
// MessageBox.Show("O(∩_∩)O 拜拜");
Instances.SettingsViewModel.Sober();
@@ -174,6 +196,19 @@ namespace MaaWpfGui.Main
_logger.Information("MaaAssistantArknights GUI exited");
_logger.Information(string.Empty);
base.OnExit(e);
}
public static void RestartApplication()
{
// 释放互斥量
_mutex?.ReleaseMutex();
_mutex?.Dispose();
// 避免 OnExit 时再次释放
_mutex = null;
System.Windows.Forms.Application.Restart();
}
/// <inheritdoc/>

View File

@@ -114,7 +114,7 @@ namespace MaaWpfGui.ViewModels.UI
iconKey: "HangoverGeometry",
iconBrushKey: "PallasBrush");
Application.Current.Shutdown();
System.Windows.Forms.Application.Restart();
Bootstrapper.RestartApplication();
}
}
@@ -2558,7 +2558,7 @@ namespace MaaWpfGui.ViewModels.UI
if (result == MessageBoxResult.OK)
{
Application.Current.Shutdown();
System.Windows.Forms.Application.Restart();
Bootstrapper.RestartApplication();
}
*/
}
@@ -2693,7 +2693,7 @@ namespace MaaWpfGui.ViewModels.UI
if (result == MessageBoxResult.OK)
{
Application.Current.Shutdown();
System.Windows.Forms.Application.Restart();
Bootstrapper.RestartApplication();
}
SetAndNotify(ref _language, value);
@@ -2763,7 +2763,7 @@ namespace MaaWpfGui.ViewModels.UI
if (result == MessageBoxResult.OK)
{
Application.Current.Shutdown();
System.Windows.Forms.Application.Restart();
Bootstrapper.RestartApplication();
}
}

View File

@@ -26,6 +26,7 @@ using System.Windows.Documents;
using System.Windows.Input;
using MaaWpfGui.Constants;
using MaaWpfGui.Helper;
using MaaWpfGui.Main;
using Markdig;
using Markdig.Wpf;
using Newtonsoft.Json;
@@ -567,7 +568,7 @@ namespace MaaWpfGui.ViewModels.UI
if (result == MessageBoxResult.OK)
{
Application.Current.Shutdown();
System.Windows.Forms.Application.Restart();
Bootstrapper.RestartApplication();
}
}