From 88a65233a7c3250e5665b81fa943633d3027772f Mon Sep 17 00:00:00 2001 From: "GukSang.Jin" Date: Mon, 16 Mar 2026 19:10:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 11 ++ AppShutdownCoordinator.cs | 65 ++++++++ Data/BaseWindow.xaml | 2 +- DetectionMenuWindow.xaml | 4 +- LoadingOverlayWindow.xaml | 187 +++++++++++++++++++++++ LoadingOverlayWindow.xaml.cs | 133 +++++++++++++++++ MainWindow.xaml | 64 ++++---- MainWindow.xaml.cs | 83 +++++++---- MainWindow2.xaml | 4 +- MainWindow2.xaml.cs | 37 ++--- MainWindow3.xaml | 4 +- MainWindow3.xaml.cs | 37 ++--- MainWindow4.xaml | 4 +- MainWindow4.xaml.cs | 37 ++--- ParameterSettingsWindow.xaml | 4 +- ParameterSettingsWindow.xaml.cs | 26 +--- ReportWindow.xaml | 4 +- ReportWindow.xaml.cs | 22 +-- ReportWindow2.xaml | 4 +- ReportWindow2.xaml.cs | 22 +-- ReportWindow3.xaml | 4 +- ReportWindow3.xaml.cs | 22 +-- ReportWindow4.xaml | 4 +- ReportWindow4.xaml.cs | 22 +-- WindowNavigationHelper.cs | 257 ++++++++++++++++++++++++++++++++ 自救器呼吸器综合检验仪.csproj | 11 +- 26 files changed, 839 insertions(+), 235 deletions(-) create mode 100644 AppShutdownCoordinator.cs create mode 100644 LoadingOverlayWindow.xaml create mode 100644 LoadingOverlayWindow.xaml.cs create mode 100644 WindowNavigationHelper.cs diff --git a/App.xaml.cs b/App.xaml.cs index c9d2066..96b4f8c 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -28,6 +28,12 @@ namespace 自救器呼吸器综合检验仪 private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { + if (AppShutdownCoordinator.ShouldSuppressDuringShutdown(e.Exception)) + { + e.Handled = true; + return; + } + LogError(e.Exception, "UI线程异常"); e.Handled = true; // 标记为已处理,避免崩溃(仅用于调试) } @@ -49,6 +55,11 @@ namespace 自救器呼吸器综合检验仪 string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "error.log"); string logContent = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {type}:\n{ex.ToString()}\n\n"; File.AppendAllText(logPath, logContent); + if (AppShutdownCoordinator.IsShutdownInProgress) + { + return; + } + MessageBox.Show($"发生错误,详情已记录到 {logPath}", "错误"); } diff --git a/AppShutdownCoordinator.cs b/AppShutdownCoordinator.cs new file mode 100644 index 0000000..a55d4d0 --- /dev/null +++ b/AppShutdownCoordinator.cs @@ -0,0 +1,65 @@ +using System; +using System.ComponentModel; +using System.Threading; +using System.Windows; +using System.Windows.Threading; + +namespace 自救器呼吸器综合检验仪 +{ + internal static class AppShutdownCoordinator + { + private static int _shutdownRequested; + + public static bool IsShutdownInProgress + { + get + { + Application application = Application.Current; + if (application == null) + { + return Interlocked.CompareExchange(ref _shutdownRequested, 0, 0) == 1; + } + + return Interlocked.CompareExchange(ref _shutdownRequested, 0, 0) == 1 + || application.Dispatcher.HasShutdownStarted + || application.Dispatcher.HasShutdownFinished; + } + } + + public static void RequestShutdown() + { + Application application = Application.Current; + if (application == null) + { + return; + } + + if (Interlocked.Exchange(ref _shutdownRequested, 1) == 1) + { + return; + } + + application.Dispatcher.BeginInvoke( + DispatcherPriority.ApplicationIdle, + new Action(() => + { + if (application.Dispatcher.HasShutdownStarted || application.Dispatcher.HasShutdownFinished) + { + return; + } + + application.Shutdown(); + })); + } + + public static bool ShouldSuppressDuringShutdown(Exception exception) + { + if (!IsShutdownInProgress || exception == null) + { + return false; + } + + return exception is InvalidOperationException || exception is Win32Exception; + } + } +} diff --git a/Data/BaseWindow.xaml b/Data/BaseWindow.xaml index 33dabf5..fc94515 100644 --- a/Data/BaseWindow.xaml +++ b/Data/BaseWindow.xaml @@ -5,7 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:自救器呼吸器综合检验仪.Data" mc:Ignorable="d" - Title="BaseWindow" Height="450" Width="800"> + Title="BaseWindow" Height="600" Width="1024" WindowState="Maximized"> diff --git a/DetectionMenuWindow.xaml b/DetectionMenuWindow.xaml index 50b435e..674999e 100644 --- a/DetectionMenuWindow.xaml +++ b/DetectionMenuWindow.xaml @@ -2,7 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:自救器呼吸器综合检验仪" - Title="检验功能选择" Height="700" Width="1200" + Title="检验功能选择" Height="600" Width="1024" WindowState="Maximized" WindowStartupLocation="CenterScreen" FontFamily="Segoe UI" Background="#F5F7FA"> @@ -127,4 +127,4 @@