c# - 如何链接对 Application.ThreadException 的调用?
问题描述
在 WinForms 应用程序 (.NET 4.5.1) 中,如果我将多个处理程序添加到Application.ThreadException
仅调用最近添加的处理程序。我在文档中的任何地方都没有看到这一点。事实上它声明,“附加你的事件处理程序......”注意“处理程序”的复数形式。
这是一个说明该行为的示例:
internal class Form1 : Form
{
[STAThread]
static void Main()
{
Application.ThreadException +=
(sender, args) => Console.WriteLine(@"This is never printed");
Application.ThreadException +=
(sender, args) => Console.WriteLine(@"Only this is printed");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Form1()
{
var button = new Button {Text = @"CRASH!"};
button.Click += (sender, args) => throw new NotImplementedException();
Controls.Add(button);
}
}
有没有办法检索现有的处理程序并从我自己的处理程序中调用它?我问的原因是我正在尝试将新的 MS AppCenter SDK 集成到我的应用程序中,并将处理程序添加到Application.ThreadException
. 我想添加自己的处理程序,但在发生异常时仍调用 AppCenter 处理程序。
这是一个伪代码,说明了我想要做什么:
static void Main()
{
AppCenter.Start("id", typeof(Crashes));
var handler = GetCurrentThreadExceptionHandler();
Application.ThreadException += (sender, args) => {
handler?.Invoke(sender, args);
Log("Something happened");
new UnhandledErrorDialog().Show();
};
}
解决方案
推荐阅读
- node.js - 如何在节点 js 结束后保持 TCP 客户端连接处于活动状态
- spring - 在从 postgres 数据库中选择中对 spring 数据与 JDBI 进行基准测试
- azure - 如何在部署作业期间为 XML 转换设置 EnvironmentName?
- atom-editor - 如何修复 Atom 窗口中的杂散垂直线
- ansible - 将剧本运行中的变量保存到 ansible 主机本地文件
- html - 如何在div内添加页眉和页脚?
- redux - 混淆 createStore 在 redux 中的工作方式
- powerbi - Power BI Card 数据对齐
- c++ - 矩阵中1的最长路径
- python - 是否有另一种解决 Pythoncom 问题的方法