Shehab Kamal <Sh*********@discussions.microsoft.comwrote:
In Main I register ThreadException to handle unhandled exceptions
Application.ThreadException += new
ThreadExceptionEventHandler(App_ThreadException);
The method will show a MessageBox or a Form instead of .NET 2 default
dialog. However I don't get the code work. What am I missing?
I don't know. You need to run outside the debugger for one thing,
because under the debugger VS hacks your application message loop and
inserts its own code into the call stack - highly frustrating, I
personally find. Also, it will only catch exceptions that are invoked on
the GUI thread via message loop dispatching - things like painting,
click events, etc.
This console application works well for me:
---8<---
using System;
using System.Windows.Forms;
static class App
{
static void Main()
{
Application.ThreadException += delegate
{
Console.WriteLine("Caught!");
};
Form form = new Form();
form.Click += delegate
{
throw new Exception();
};
Application.Run(form);
}
}
--->8---
One silly question. Can I post .NET 2 questions here or should I post to
msdn2?
..NET 2 is .NET as well.
-- Barry
--
http://barrkel.blogspot.com/