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

标签 wpf exception-handling

我正在尝试在我的 WPF 应用程序中捕获损坏的状态异常 (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。

关于WPF 损坏状态异常。如何实现 HandleProcessCorruptedStateExceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5355768/

相关文章:

WPF - 关闭 App.g.cs 中 Main 的自动生成

c# - 如何在 wpf 中制作多色分段进度条?

c# - MVVMLight Simple IOC - 动态注册和注销数据服务实现

c# - WPF ResizeMode 改变窗口大小

architecture - 为什么 Repository Pattern 的例子从不处理数据库连接异常?

.net - 如何正确处理ThreadInterruptedException?

python - Python错误异常

wpf - DataGrid:仅在保存第一行的更改后才允许编辑第二行

c# - 在ASP.NET C#项目中实现异常处理

ruby-on-rails - AirBrake vs Exceptional vs errbit : which one is the best to track exceptions in your rails app?