【问题标题】:WPF Corrupted state exceptions. How to implement HandleProcessCorruptedStateExceptionsWPF 损坏状态异常。如何实现 HandleProcessCorruptedStateExceptions
【发布时间】:2011-03-18 17:36:13
【问题描述】:

我正在尝试在我的 WPF 应用程序中捕获损坏的状态异常 (CES)。我只是想在退出之前记录错误。我的应用程序使用旧版 Win32/COM dll,因此需要捕获这些。我捕捉这些的代码如下。 (我在几个地方添加了 HandleProcessCorruptedStateExceptions,因为它在处理程序本身中不起作用)。生成崩溃的 sn-p 位于处理程序下方。但是我仍然看到系统错误对话框,并且我的处理程序永远不会触发...任何帮助表示赞赏

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();
    }
}

【问题讨论】:

    标签: wpf exception-handling


    【解决方案1】:

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

    阅读更多内容,似乎 .NET 3.5 和 4.0 之间的行为发生了变化,因此您可能想尝试该文章中所写的内容

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

    【讨论】:

      猜你喜欢
      • 2023-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2017-02-18
      • 2012-02-16
      • 2019-08-31
      • 1970-01-01
      相关资源
      最近更新 更多