当前位置: 首页 > news >正文

WPF 全局异常处理

在Application中存在三种异常事件EventHandler

  • DispatcherUnhandledException
  • AppDomain.CurrentDomain.UnhandledException
  • TaskScheduler.UnobservedTaskException

其中 DispatcherUnhandledException 是在异常由应用程序引发但未进行处理时发生,但无法捕获多线程异常
AppDomain.CurrentDomain.UnhandledException专门捕获所有线程中的异常(不包括Task)
TaskScheduler.UnobservedTaskException 捕获Task中的异常

这些异常Handler可以在应用程序出现异常是记录日志,或者挽回应用程序奔溃的问题。

举例说明

以下异常会触发 DispatcherUnhandledException 以及 AppDomain.CurrentDomain.UnhandledException
执行顺序是 DispatcherUnhandledException => AppDomain.CurrentDomain.UnhandledException

int x = 0;
_ = 1 / x;

以下异常会触发 AppDomain.CurrentDomain.UnhandledException

new Thread(() => { _ = 1 / x; }).Start();

以下异常会触发 TaskScheduler.UnobservedTaskException
Task中的异常并不是立刻就能捕获到的,而是等到垃圾回收的时候进行捕获。如果想立刻进行捕获则可以调用GC.Collect(0);和GC.WaitForPendingFinalizers();

Task.Run(() => { _ = 1 / x; });

以下是在Prism框架下的异常处理,其中 Task异常不会导致应用程序奔溃,
DispatcherUnhandledException异常可以通过e.Handled = true;表明该异常已被处理,不会造成程序崩溃和退出。
AppDomain.CurrentDomain.UnhandledException 在.Net FrameWork中可以通过设置在 App.config <runtime> 节点下添加 <legacyUnhandledExceptionPolicy enabled="1"/> 可以阻止应用程序奔溃,但是这边我使用的是Net6.0所以没成功!

public partial class App{protected override Window CreateShell(){return Container.Resolve<MainWindow>();}protected override void OnInitialized(){#region 全局异常事件配置// 在异常由应用程序引发但未进行处理时发生,但无法捕获多线程异常// UI线程中的异常 UnhandledException 和  DispatcherUnhandledException 都会捕获 执行顺序是 DispatcherUnhandledException => UnhandledExceptionthis.DispatcherUnhandledException += App_DispatcherUnhandledException;// 专门捕获所有线程中的异常 AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;// 专门捕获Task异常TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;#endregion// 初始化menu 并导航到一个页面IStartService startService = App.Current.MainWindow.DataContext as IStartService;startService.Start();base.OnInitialized();}private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e){Console.WriteLine("---捕获Task异常---");e.SetObserved();}private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e){Console.WriteLine("---所有线程中的异常---");MessageBox.Show((e.ExceptionObject as Exception).Message);//在.Net6.0中无效 --- 在 app.config <runtime> 节点下添加 <legacyUnhandledExceptionPolicy enabled="1"/> 可以阻止应用程序奔溃类似 e.Handle=true}private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e){MessageBox.Show("应用程序异常:" + e.Exception.Message);// 表明该异常已被处理,不会造成程序崩溃和退出e.Handled = true;}protected override void RegisterTypes(IContainerRegistry containerRegistry){}protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog){// 添加Coding模块moduleCatalog.AddModule<CodingModuleModule>();base.ConfigureModuleCatalog(moduleCatalog);}}
http://www.lryc.cn/news/264969.html

相关文章:

  • Flink系列之:Elasticsearch SQL 连接器
  • java中将Map集合、对象、字符串转换为JSON对象
  • 理解Spring中bean的作用域
  • edge中以右键“打印”的方式“保存”当前页面的pdf形式,下载过程中卡进度的问题
  • c# 使用OpenCV
  • 数据库连接问题 - ChatGPT对自身的定位
  • 常见可视化大屏编辑器有哪些?
  • 利用ffmpeg cv2取h265码流视频(转换图片灰屏问题解决)
  • Android Uri scheme协议file转content
  • 【Jenkins】远程API接口:Java 包装接口使用示例
  • 未能加载工具箱项问题的解决
  • 算法模板之栈图文详解
  • Ajax Search Pro Live WordPress网站内容实时搜索插件
  • mysql SQL执行超时问题
  • 51单片机基于时间片轮转的简单rtos
  • python pycurl 安装使用
  • C语言数据结构-排序
  • Spring AOP入门指南:轻松掌握面向切面编程的基础知识
  • 【顶级快刊】IEEE(Trans),审稿快仅2个月录用,入选CCF-B,现在投最快!
  • 深入浅出堆排序: 高效算法背后的原理与性能
  • Golang实践录:gin绑定解析json的两种方法
  • Hypervisor Display架构
  • 基于ssm二手车交易平台的设计论文
  • IDEA 设置 SpringBoot logback 彩色日志(附配置文件)
  • 数学建模学习笔记-皮尔逊相关系数
  • 随笔:集成学习:关于随机森林,梯度提升机的东拉西扯
  • 多款实用个人年终总结模板,助力你的年度汇报!
  • 【C语言】动态内存管理基础知识——动态通讯录,如何实现通讯录容量的动态化
  • Centos9(Stream)配置Let‘s Encrypt (免费https证书)
  • Spring之事务(2)