这是这篇文章后面遗留的问题:
winform中的全局异常信息_winform全局异常捕获_zxy2847225301的博客-CSDN博客
就是线程抛出异常后,被AppDomain.CurrentDomain.UnhandledException注册的事件捕获后,程序依旧崩溃退出。
解决方案:在app.config配置文件中的runtime节点中加上<legacyUnhandledExceptionPolicy enabled="1"/>
<runtime>
    <legacyUnhandledExceptionPolicy enabled="1"/>
    <!--<legacyCorruptedStateExceptionsPolicy enabled="true"/>-->
</runtime>-------------------废话开始,可以跳过不看
趁着今天有空,就找找有没有对应的处理方案。
首先是翻看了现在所在公司的代码,使用的是winform程序,没找到对应的解决方案。再翻看上一家公司的代码,使用的是wpf,看到注册的是Application.Current.DispatcherUnhandledException,然后在该事件中设置e.Handled = true;如下图:

 
 
但是查阅资料后发现,这种是UI线程抛出的异常处理, 对于非UI线程抛出的异常,还是需要用AppDomain.CurrentDomain.UnhandledException来捕捉
依旧没找到解决的方案,上一家另外一个系统的代码就无法获取到了,但我看过,记得是在AppDomain.CurrentDomain.UnhandledException注册的事件中通过代码的方式Dump出文件,但程序依旧还是会崩溃。
最后只能是网上不停地根据关键词去搜索,可能搜索的关键词不对,搜索了一段时间依旧没找到对对应的解决方案。没办法,只能看看AppDomain.CurrentDomain.UnhandledException注册的事件中的方法参数,看到有一个参数e(类型为UnhandledExceptionEventArgs)中一个属性isTerminating,指示公共语言运行时是否即将终止,如下图:

很可惜,这个属性只能是获取而不能设置。再反编译看看它的构造函数,如下图:

再打个断点调试一下,发现还是不行。只能是根据关键词isTerminating去stack overflow去搜搜看有没有解决方案了。还真找到了关键词:legacyUnhandledExceptionPolicy
链接如下:
c# - What causes the UnhandledExceptionEventArgs.IsTerminating flag to be true or false? - Stack Overflow

再通过关键词legacyUnhandledExceptionPolicy去搜索就发现很多博文了。
-------------------废话结束,可以跳过不看
好了,废话到此结束,正式开始本文的内容
测试环境:
vistual studio 2017
winform
.net framework 4..7
步骤如下:
1 新增winfform程序,名为WindowsFormsApp1,在默认建好的窗体Form1中拖入一个Button按钮,并编写代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem((a) => {
                throw new Exception("测试抛出异常");
            });
        }
    }
}
2 在Program程序中编写代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
           Application.Run(new Form1());
        }
       
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
           
            MessageBox.Show("捕获到异常信息");
        }
    }
}
3 新增配置文件App.config,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <legacyUnhandledExceptionPolicy enabled="1"/>
    <!--<legacyCorruptedStateExceptionsPolicy enabled="true"/>-->
  </runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/></startup></configuration>
4 然后编译并生成,运行结果如下:

可以看到,程序发生异常后,并不会崩溃退出。
最后插一句,反编译工具dnspy的配置文件使用的是<legacyCorruptedStateExceptionsPolicy enabled="true" />
 
 
但我试了一下,并不好使,改变.net framework的版本都试过,不好使,可能是我操作的姿势不对吧。










![[性能测试工具]——Loadrunner的使用及安装指南](https://img-blog.csdnimg.cn/41882bc22a1948c0a4e3cd31c4235fbe.png)








