WPF程序启动崩溃:如何调试?

Dav*_*Ong 6 c# debugging wpf

我有一个在开发PC和客户端PC上运行良好的WPF程序.但是在客户端PC 2上,它在启动时立即崩溃并发送到Microsoft窗口.我希望得到一些关于如何追查错误的建议.这是我尝试过的:

  1. 在我的主窗口类中插入了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()之前甚至没有出现.所以看起来应用程序甚至在启动我的代码之前就崩溃了.

  1. 一种可能性是将整个VS2008安装在客户端PC 2中,加载源并使用IDE调试器来跟踪问题.这可能是发现问题最有效的方法.但我不想这样做因为a)客户端PC 2不属于我,b)它不能扩展:我必须为客户端PC 3/4/5/...做同样的事情,并且c)它违反了我的公司的VS2008许可证.

我该如何调试此问题?

J T*_*ana 6

老派方法:像这样的严重崩溃可能会冒泡到您可以通过 Windows 中的事件查看器看到的东西。你去那里检查了吗?很多时候这告诉我答案,没有任何额外的麻烦。


Jef*_*ght 5

在您的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)