【发布时间】: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();
}
}
【问题讨论】: