Carl
Carl

Reputation: 1254

WPF application has encountered a problem and needs to close, how to get good message?

I am currently working on a WPF application that processes data in a database and it worked fine when I tested it with a test database with around 1000 rows but now I want to process around 50 000 rows and it just crashes during the processing giving me "'application' has encountered a problem and needs to close ... Do you want to send error report to Microsoft...".

Now this may seem like a duplicate of a couple of other questions but I've tried AppDomain.CurrentDomain.UnhandledException, AppDomain.CurrentDomain.ProcessExit, Application.DispatcherUnhandledException and it just crashes with no message anyway.

Here's what it looks like:

private void OnStartup(object sender, StartupEventArgs e)
{
  AppDomain.CurrentDomain.UnhandledException += (s, args) =>
  {
    MessageBox.Show("Message1");
  };

  AppDomain.CurrentDomain.ProcessExit += (s, args) =>
  {
    MessageBox.Show("Message2");
  };

  this.DispatcherUnhandledException += (s, args) =>
  {
    MessageBox.Show("Message3");
  };

  try
  {
    Views.MainView view = new Views.MainView();
    view.DataContext = new ViewModels.MainViewModel();
    view.ShowDialog();
  }
  catch (Exception exception)
  {
    MessageBox.Show(exception.ToString());
  }
}

The processing is started by a command (a GUI button with command binding to call this):

public void Process()
{
  Thread workerThread = new Thread(new ThreadStart(DoProcess));
  workerThread.IsBackground = true;
  workerThread.Start();
}

private void DoProcess()
{
  try
  {
    // Most probably crashes during that
    DoStuff();
  }
  catch (Exception e)
  {
    System.Windows.MessageBox.Show(e.ToString());
  }
}

I checked the memory usage during execution and it never goes above 70 meg, so that shouldn't be a problem. I use 2 COM components, one that is an .exe and another that is a .dll, I don't know if problems can come from that. Note that sometimes those components crash and I do receive a useful error message when it happens. I work with NHibernate to read/write from the database.

For now I have absolutely no clue what the problem is (the DoStuff() part is quite big), and I don't understand how an error can get past all those exception handlers and just crash the application without information. Note that I can't debug the application in Visual Studio because I run the application on a VM that doesn't have Visual Studio installed (and I've been waiting for 2 months to get my IT department to install me sql server so that I can run my application on my computer).

Any help would be appreciated!

Upvotes: 1

Views: 1249

Answers (2)

Ed Bayiates
Ed Bayiates

Reputation: 11230

Your DoProcess function is in a background thread. Another thread may be crashing, or you might have an issue calling MessageBox from a background thread because it's not the UI thread -- so you may be getting an exception, and then a new exception is thrown from your catch because of the MessageBox call. Try inserting code to write the message to a file before the MessageBox call and see if that works, or even a Console.Beep() for that matter and listen for it :-) If that uncovers nothing, try adding log statements to files at many places in your code until you narrow down where the crash occurs.

Upvotes: 1

Scott C Wilson
Scott C Wilson

Reputation: 20046

You really need to get Visual Studio and the debugger set up. You'll be probing in the dark without it.

Upvotes: 0

Related Questions