From b16ec37df351f101b88c7d30fa68569cb4ac7ef5 Mon Sep 17 00:00:00 2001 From: CJKmkp <2564608840@qq.com> Date: Thu, 19 Feb 2026 18:28:01 +0800 Subject: [PATCH] =?UTF-8?q?improve:=E5=AE=89=E5=85=A8=E9=9D=A2=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helpers/ProcessProtectionManager.cs | 33 ++++++++++++-- Ink Canvas/MainWindow.xaml.cs | 44 ++++++++++++++++--- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/Ink Canvas/Helpers/ProcessProtectionManager.cs b/Ink Canvas/Helpers/ProcessProtectionManager.cs index 37286743..97553d96 100644 --- a/Ink Canvas/Helpers/ProcessProtectionManager.cs +++ b/Ink Canvas/Helpers/ProcessProtectionManager.cs @@ -64,11 +64,20 @@ namespace Ink_Canvas.Helpers return; } - var waitStart = Environment.TickCount; - while (Interlocked.CompareExchange(ref _writeGate, 1, 0) != 0) + const int gateTimeoutMs = 10_000; + if (!TryEnterWriteGate(gateTimeoutMs)) { - if (Environment.TickCount - waitStart < 2000) Thread.Sleep(10); - else Thread.Sleep(50); + try + { + LogHelper.WriteLogToFile($"ProcessProtectionManager.WithWriteAccess: 获取写入门闩超时({gateTimeoutMs}ms),将降级直接执行写入动作。目标: {targetPath}", + LogHelper.LogType.Warn); + } + catch + { + } + + action(); + return; } var normalized = NormalizePath(targetPath); @@ -134,6 +143,22 @@ namespace Ink_Canvas.Helpers } } + private static bool TryEnterWriteGate(int timeoutMs) + { + if (timeoutMs <= 0) timeoutMs = 1; + + var start = Environment.TickCount; + while (Interlocked.CompareExchange(ref _writeGate, 1, 0) != 0) + { + var elapsed = unchecked(Environment.TickCount - start); + if (elapsed >= timeoutMs) return false; + + if (elapsed < 2000) Thread.Sleep(10); + else Thread.Sleep(50); + } + return true; + } + private static void Enable() { Enable(rescanRoot: true, rescanDirs: null); diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index a9a87c7e..a442afab 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -1388,9 +1388,19 @@ namespace Ink_Canvas } } - private async void Window_Closing(object sender, CancelEventArgs e) + private bool _allowCloseAfterExitVerification; + private bool _isExitVerificationInProgress; + + private void Window_Closing(object sender, CancelEventArgs e) { LogHelper.WriteLogToFile("Ink Canvas closing", LogHelper.LogType.Event); + + if (_allowCloseAfterExitVerification) + { + _allowCloseAfterExitVerification = false; + return; + } + try { // 快抽按钮现在集成在主窗口中,不需要单独关闭 @@ -1404,13 +1414,33 @@ namespace Ink_Canvas { if (!App.IsUpdateInstalling && SecurityManager.IsPasswordRequiredForExit(Settings)) { - bool ok = await SecurityManager.PromptAndVerifyAsync(Settings, this, "退出验证", "请输入安全密码以退出软件。"); - if (!ok) + e.Cancel = true; + if (_isExitVerificationInProgress) return; + + _isExitVerificationInProgress = true; + Dispatcher.BeginInvoke(new Action(async () => { - e.Cancel = true; - LogHelper.WriteLogToFile("Ink Canvas closing cancelled by security password", LogHelper.LogType.Event); - return; - } + try + { + bool ok = await SecurityManager.PromptAndVerifyAsync(Settings, this, "退出验证", "请输入安全密码以退出软件。"); + if (!ok) + { + LogHelper.WriteLogToFile("Ink Canvas closing cancelled by security password", LogHelper.LogType.Event); + return; + } + + _allowCloseAfterExitVerification = true; + Close(); + } + catch + { + } + finally + { + _isExitVerificationInProgress = false; + } + }), DispatcherPriority.Normal); + return; } } catch