diff --git a/src/MaaWpfGui/App.xaml.cs b/src/MaaWpfGui/App.xaml.cs index 7eedce5aba..bf3b6a30ad 100644 --- a/src/MaaWpfGui/App.xaml.cs +++ b/src/MaaWpfGui/App.xaml.cs @@ -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); - } } } diff --git a/src/MaaWpfGui/Main/Bootstrapper.cs b/src/MaaWpfGui/Main/Bootstrapper.cs index 6338ba8365..fdfe8d26fb 100644 --- a/src/MaaWpfGui/Main/Bootstrapper.cs +++ b/src/MaaWpfGui/Main/Bootstrapper.cs @@ -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 { private static ILogger _logger = Logger.None; + private static Mutex _mutex; /// /// 初始化些啥自己加。 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 /// 退出时执行啥自己加。 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(); } /// diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index e9535990a9..a7804c24db 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -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(); } } diff --git a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs index b6d0eded52..0b8480de13 100644 --- a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs @@ -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(); } }