Bläddra i källkod

【标注端】&【审核端】添加捕捉全局报错

xuan.wang 8 månader sedan
förälder
incheckning
eb3ebc439f
2 ändrade filer med 53 tillägg och 7 borttagningar
  1. 27 1
      aiplabeler/App.xaml.cs
  2. 26 6
      aipreviewer/App.xaml.cs

+ 27 - 1
aiplabeler/App.xaml.cs

@@ -1,4 +1,7 @@
-using System.Windows;
+using AIPlatform.Protocol.Utilities;
+using System;
+using System.Windows;
+using System.Windows.Threading;
 
 namespace aiplabeler
 {
@@ -7,5 +10,28 @@ namespace aiplabeler
     /// </summary>
     public partial class App : Application
     {
+        protected override void OnStartup(StartupEventArgs e)
+        {
+            // 添加对AppDomain中未处理异常的处理程序
+            AppDomain.CurrentDomain.UnhandledException += OnDomainOnUnhandledException;
+            // 添加对Dispatcher中未处理异常的处理程序
+            Current.DispatcherUnhandledException += OnDispatcherUnhandledException;
+        }
+
+        private void OnDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
+        {
+            // 此时不能执行UI操作,因为这不是在UI线程上
+            Exception exception = (Exception)e.ExceptionObject;
+            Logger.WriteLineError($"OnDomainOnUnhandledException: {exception}");
+        }
+
+        private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
+        {
+            // 可以在这里执行UI操作,因为这是在UI线程上
+            Exception exception = e.Exception;
+            Logger.WriteLineError($"OnDispatcherUnhandledException: {exception}");
+            MessageBox.Show($"An unexpected error has occurred.\r\n{exception.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+            e.Handled = true; // 如果设置为true,则不会关闭应用程序
+        }
     }
 }

+ 26 - 6
aipreviewer/App.xaml.cs

@@ -1,10 +1,7 @@
-using System;
-using System.Collections.Generic;
-using System.Configuration;
-using System.Data;
-using System.Linq;
-using System.Threading.Tasks;
+using AIPlatform.Protocol.Utilities;
+using System;
 using System.Windows;
+using System.Windows.Threading;
 
 namespace aipreviewer
 {
@@ -13,5 +10,28 @@ namespace aipreviewer
     /// </summary>
     public partial class App : Application
     {
+        protected override void OnStartup(StartupEventArgs e)
+        {
+            // 添加对AppDomain中未处理异常的处理程序
+            AppDomain.CurrentDomain.UnhandledException += OnDomainOnUnhandledException;
+            // 添加对Dispatcher中未处理异常的处理程序
+            Current.DispatcherUnhandledException += OnDispatcherUnhandledException;
+        }
+
+        private void OnDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
+        {
+            // 此时不能执行UI操作,因为这不是在UI线程上
+            Exception exception = (Exception)e.ExceptionObject;
+            Logger.WriteLineError($"OnDomainOnUnhandledException: {exception}");
+        }
+
+        private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
+        {
+            // 可以在这里执行UI操作,因为这是在UI线程上
+            Exception exception = e.Exception;
+            Logger.WriteLineError($"OnDispatcherUnhandledException: {exception}");
+            MessageBox.Show($"An unexpected error has occurred.\r\n{exception.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+            e.Handled = true; // 如果设置为true,则不会关闭应用程序
+        }
     }
 }