I use this code to catch the WinForm application UnhandledException.
[STAThread]
static void Main(string[] args)
{
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
// Set the unhandled exception mode to force all Windows Forms errors
// to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
try
{
Application.Run(new MainForm());
} catch....
There I will try to restart the application. Now my problem is to simulate a exception like this. I tried before try (in main): throw new NullReferenceException("test");
VS caught it.
Tried also in MainForm code with button :
private void button1_Click(object sender, EventArgs ev)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(TestMe), null);
}
protected void TestMe(object state)
{
string s = state.ToString();
}
did not help, VS caught it, even in Release mode.
- How should I, finally, force the application generate
UnhandleldException
? - Will I be able to restart the application in
CurrentDomain_UnhandledException
? - How can I generate a
ThreadException
?
PS.
If I launch outside the VS a windows generic window
Application MyApplication" encountered a error and should be closed...blabla...Send report/Don't send.
I want, however, VS enter this method (...Domain_Unhahdled...)
EDIT: When restarting the application, Is it possible to disable the windows crash message appearing like: ?
Code:
static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
// Since we can't prevent the app from terminating
// log this to the event log.
Logger.LogMessage(ERROR, errorMsg);
Application.Restart();