我有一个在开发PC和客户端PC上运行良好的WPF程序.但是在客户端PC 2上,它在启动时立即崩溃并发送到Microsoft窗口.我希望得到一些关于如何追查错误的建议.这是我尝试过的:
在我的主窗口类中插入了try-catch:
public MainWindow()
{
try
{
MessageBox.Show("Before InitComp()");
InitializeComponent();
MessageBox.Show("Before Sub1()");
Subroutine1();
MessageBox.Show("Before Sub2()");
Subroutine2();
... etc ...
}
catch (Exception ex)
{ ... code for MessageBox display error here ... }
}
Run Code Online (Sandbox Code Playgroud)我的想法是尝试隔离启动序列的哪个部分崩溃,但第一个调试消息"在InitComp()之前甚至没有出现.所以看起来应用程序甚至在启动我的代码之前就崩溃了.
我该如何调试此问题?
在您的App.xaml标头中,添加:
<Application DispatcherUnhandledException="App_DispatcherUnhandledException" />
Run Code Online (Sandbox Code Playgroud)
然后在您的App.xaml.cs中添加以下内容:
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
{
log.Fatal("An unexpected application exception occurred", args.Exception);
MessageBox.Show("An unexpected exception has occurred. Shutting down the application. Please check the log file for more details.");
// Prevent default unhandled exception processing
args.Handled = true;
Environment.Exit(0);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5763 次 |
| 最近记录: |