Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Program Startup Crash: How to Debug?

Tags:

c#

debugging

wpf

I have a WPF program that runs fine on the development PC and on the client PC 1. But on client PC 2, it crashes immediately on startup with the Send Report to Microsoft window. I would appreciate some advice on how to trace what is wrong. Here's what I have tried:

  1. Inserted try-catch in my main window class:

    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 ... }
    }
    

The idea is to try to isolate which part of the startup sequence is crashing, but the first debug message "Before InitComp()" does not even show up. So it seems the app is crashing even before starting my code.

  1. One possibility is to install the entire VS2008 in the client PC 2, load in the source and use the IDE Debugger to trace the problem. This is likely the most effective in finding the problem. But I do not want to do this because a) the client PC 2 does not belong to me, b) it does not scale: I must do likewise for client PC 3/4/5/... and c) it violates my firm's VS2008 license.

How should I go about debugging this problem?

like image 626
David Ong Avatar asked Oct 19 '11 03:10

David Ong


2 Answers

In your App.xaml header, add:

<Application DispatcherUnhandledException="App_DispatcherUnhandledException" />

and in your App.xaml.cs, add something like:

    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);
    }
like image 124
Jeffrey Knight Avatar answered Sep 20 '22 18:09

Jeffrey Knight


Old school approach: A hard crash like that is probably bubbling out to something you can see via Event Viewer in Windows. Have you checked there yet? A lot of times this tells me the answer without any extra trouble.

like image 22
J Trana Avatar answered Sep 17 '22 18:09

J Trana