WPF损坏的状态异常.如何实现HandleProcessCorruptedStateExceptions

我想在我的
WPF应用程序中捕获Correupted状态异常(CES).我只是想在退出之前记录错误.我的应用程序使用传统的Win32 / COM dll因此需要捕获这些.我的代码捕获这些是在下面. (我在一些地方添加了HandleProcessCorruptedStateExceptions,因为它在处理程序本身中不起作用).生成崩溃的片段位于处理程序下方.但是,我仍然看到系统错误对话框,我的手枪从不开火…任何帮助表示赞赏

public partial class App : Application
{
    [HandleProcessCorruptedStateExceptions]
    [SecurityCritical]
    protected override void OnStartup(StartupEventArgs e) 
    { 
        base.OnStartup(e);
        AppDomain.CurrentDomain.FirstChanceException += new EventHandler<FirstChanceExceptionEventArgs>(CurrentDomain_FirstChanceException);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
    }

    [HandleProcessCorruptedStateExceptions]
    [SecurityCritical]
    void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        EatIt();
    }

    [HandleProcessCorruptedStateExceptions]
    [SecurityCritical]
    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        EatIt();
    }

    [HandleProcessCorruptedStateExceptions]
    [SecurityCritical]
    void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
    {
        EatIt();
    }
    private void EatIt()
    {
        // Add some kind of logging then terminate...
    }
}

生成崩溃的片段

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        CrashIt();
    }
    unsafe static void CrashIt()
    {
        var obj = new byte[1];
        var pin = GCHandle.Alloc(obj, GCHandleType.Pinned);
        byte* p = (byte*)pin.AddrOfPinnedObject();
        for (int ix = 0; ix < 256; ++ix) *p-- = 0;
        GC.Collect();

    }
}

我已经修改了启动代码,用try / catch子句包含了应用程序.仍然没有成功.有谁真的知道如何让这些东西工作??? (我仍然得到Windows错误对话框)

public class EntryPoint
{
    // All WPF applications should execute on a single-threaded apartment (STA) thread
    [STAThread]
    [HandleProcessCorruptedStateExceptions]
    [SecurityCritical]
    public static void Main()
    {
        CustomApplication app = new CustomApplication();
        try
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            app.Run();
        }
        catch (Exception)
        {
            System.Diagnostics.Debug.WriteLine("xx");
        }
    }

    [HandleProcessCorruptedStateExceptions]
    [SecurityCritical]
    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("xx");
    }
}
public class CustomApplication : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        MainWindow window = new MainWindow();
        window.Show();
    }
}

最佳答案 阅读
corrupted state exceptions部分,在我看来,实际执行try..catch的方法需要应用属性.您正在为某些事件注册处理程序,因此您的属性不起作用.

再看一遍,似乎.NET 3.5和4.0之间的行为发生了变化,所以你可能想尝试一下这篇文章所写的内容

可能有用的是自己编写一个入口点,并使用Initialize / Run启动WPF应用程序,然后标记入口点方法并在运行时编写try / catch.

点赞