From 80beea1297acdf3dc85fd954974e94e9bf93ec67 Mon Sep 17 00:00:00 2001 From: uye <2396806385@qq.com> Date: Sun, 14 May 2023 02:46:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0=E5=90=8E=E5=9B=A0=E4=BA=92?= =?UTF-8?q?=E6=96=A5=E9=94=81=E9=81=87=E5=88=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/App.xaml.cs | 35 ------------------- src/MaaWpfGui/Main/Bootstrapper.cs | 35 +++++++++++++++++++ .../ViewModels/UI/SettingsViewModel.cs | 8 ++--- .../ViewModels/UI/VersionUpdateViewModel.cs | 3 +- 4 files changed, 41 insertions(+), 40 deletions(-) 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(); } }