Application.ThreadException not working

J

joseph_gallagher

Hi,

I have a shoddily written application that is written in 1.1 that I've
ported to 2.0 and now occasionaly I get crashes which I suspect are
been caused by exceptions in threads, most likely due to trying to
access controls which is now stricter, I know I could just set

CheckForIllegalCrossThreadCalls = false;

But I'd rather like to understand a bit better about ways to handle
these exceptions and diagnose them so have been playing with
"Application.ThreadException" and
"AppDomain.CurrentDomain.UnhandledException", the problem I have is
"Application.ThreadException" never seems to fire and this is cunfusing
the hell out of me. I have the following simple test code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TestApp {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
Application.ThreadException += new
ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}

void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e) {
MessageBox.Show("CurrentDomain_UnhandledException " +
e.ExceptionObject.ToString());

}

void Application_ThreadException(object sender,
ThreadExceptionEventArgs e) {
MessageBox.Show("Application_ThreadException");
}

private void exceptionButton_Click(object sender, EventArgs e)
{
Thread t = new Thread(delegate() {
int zero = 0;
int x = 1 / zero;
});
t.IsBackground = true;
t.Start();
}

private void crossThread_Click(object sender, EventArgs e) {
Thread t = new Thread(delegate() {
crossThread.Text = "Error";
});
t.IsBackground = true;
t.Start();
}
}
}

Which is just a windows form that has 2 buttons, crossThreadButton &
exceptionButton, crossThreadButton simply creates a thread that tries
to access a control it shouldn't, and exceptionButton simply divides by
0 to create a exception, both cause the
"AppDomain.CurrentDomain.UnhandledException" event to be fired, but
niether cause the "Application.ThreadException" event to be fired which
is confusing me.

When should "Application.ThreadException" get called, am I missing a
setting or is it only for specific situations.

Also is there a way to prevent the application from exiting when there
is a exception in a thread and
"AppDomain.CurrentDomain.UnhandledException" is fired, In my main
shoddy app I'm 99.99% sure that exceptions in threads that have a long
lifetime are handled and that these problems are been caused in
shortlived threads that if they fail the work will simply be picked up
on the next iteration, there is a reasonable amount of manual work
involved in setting up and configuring the app and loosing all this is
a pain for the user, if they could be informed of the error but allowed
to continue that would be a lot easier for me while I find all the
causes of the problem.
 
G

Guest

try calling:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);


Ciaran O'Donnell
 
D

David Levine

You are subscribing to the events in the .ctor of the form itself. You need
to subscribe to them before the Form is created, like in your Main, or
anywhere before the form itself is created.

The reason has to do with the way the Forms class wraps the window
procedure with its own try-catch and then decides whether or not to handle
the exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top