ThreadException event does not in Main()

A

Andrus

T reproduce, run code.

Observed: Vista dialog Myapp stops working

Expected: Messagebox.

If RaiseTypeLoadException() is removed from Main(), Messagebox occurs.
ThreadException event does not occur before MainForm is instantiated.

How to fix ?

Andrus.

using System;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;

class Test
{
static void Main()
{
Application.ThreadException += Application_ThreadException;
RaiseTypeLoadException();
Application.Run(new MainForm());
}

class MainForm : Form
{
protected override void OnLoad(EventArgs e)
{
RaiseTypeLoadException();
base.OnLoad(e);
}

}

public static void RaiseTypeLoadException()
{
Assembly asm = Assembly.GetEntryAssembly();
asm.GetType("Bad", true);
}


static void Application_ThreadException(object sender,
ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
}
}
 
G

Göran Andersson

Andrus said:
T reproduce, run code.

Observed: Vista dialog Myapp stops working

Expected: Messagebox.

If RaiseTypeLoadException() is removed from Main(), Messagebox occurs.
ThreadException event does not occur before MainForm is instantiated.

How to fix ?

Andrus.

As I see it, it works exactly as expected.

The Application.ThreadException event occurs when an exception occurs in
a Windows Forms thread.

If you cause an exception in code that is not run from a form, that
should not cause the Application.ThreadException event to be triggered.
 

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