If I assign ThreadExceptionEventHandler
to Application.ThreadException
, why when calling a delegate method using a control in the main thread of the application there are exceptions that this delegate throws out without starting the event handler?
i.e.
static void Main()
{
...
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
Console.Error.Write("A thread exception occurred!");
}
...
private void Form1_Load(object sender, EventArgs e)
{
Thread syncThread = new Thread(new ThreadStart(this.ThrowException));
syncThread.Start();
}
private void ThrowException()
{
button1.Invoke(new MethodInvoker(delegate
{
throw new Exception();
}));
}
The context is that I have a background thread running from a form that throws an unhandled exception that terminates the application. I know that this thread will be unreliable, because it is connected to network connections and therefore can be terminated at any time, but I'm just wondering why this script does not look the way I expect?
source
share