Hi,
This is my Program class in my Application -
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
// Subscribe to thread (unhandled) exception events
ThreadException Handler handler = new
ThreadException Handler();
Application.Thr eadException += new
ThreadException EventHandler(ha ndler.Applicati onThreadExcepti on);
Application.Run (new FormMain());
}
/// <summary>
/// Handles any thread exceptions
/// </summary>
public class ThreadException Handler
{
// set when there is an unhandled exception. Can therefore
be used by FormMain.
static bool unhandeledExcep tion = false;
public static bool UnhandeledExcep tion { get { return
unhandeledExcep tion; } }
public void ApplicationThre adException(obj ect sender,
ThreadException EventArgs e)
{
try
{
unhandeledExcep tion = true;
ShowThreadExcep tionDialog(e.Ex ception);
}
finally
{
// Fatal error, terminate program
Application.Exi t();
}
}
/// <summary>
/// Creates and displays the error message.
/// </summary>
private void ShowThreadExcep tionDialog(Exce ption ex)
{
from with FormMain I do the following if there is an exception which
is to not save the users settings -
private void OnClosing(objec t sender, EventArgs ev)
{
if (Program.Thread ExceptionHandle r.UnhandeledExc eption)
return;
// save user settings...
}
The problem though is that if I create a thread as follows, exceptions
are not being handled by ThreadException Handler -
public void FormMain_Load(o bject sender, EventArgs e)
{
thread = new System.Threadin g.Thread(MyThre ad);
thread.Start();
}
void MyThread()
{
throw new ApplicationExce ption("My exception");
}
Why not?
Thanks as ever,
Aine
This is my Program class in my Application -
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
// Subscribe to thread (unhandled) exception events
ThreadException Handler handler = new
ThreadException Handler();
Application.Thr eadException += new
ThreadException EventHandler(ha ndler.Applicati onThreadExcepti on);
Application.Run (new FormMain());
}
/// <summary>
/// Handles any thread exceptions
/// </summary>
public class ThreadException Handler
{
// set when there is an unhandled exception. Can therefore
be used by FormMain.
static bool unhandeledExcep tion = false;
public static bool UnhandeledExcep tion { get { return
unhandeledExcep tion; } }
public void ApplicationThre adException(obj ect sender,
ThreadException EventArgs e)
{
try
{
unhandeledExcep tion = true;
ShowThreadExcep tionDialog(e.Ex ception);
}
finally
{
// Fatal error, terminate program
Application.Exi t();
}
}
/// <summary>
/// Creates and displays the error message.
/// </summary>
private void ShowThreadExcep tionDialog(Exce ption ex)
{
from with FormMain I do the following if there is an exception which
is to not save the users settings -
private void OnClosing(objec t sender, EventArgs ev)
{
if (Program.Thread ExceptionHandle r.UnhandeledExc eption)
return;
// save user settings...
}
The problem though is that if I create a thread as follows, exceptions
are not being handled by ThreadException Handler -
public void FormMain_Load(o bject sender, EventArgs e)
{
thread = new System.Threadin g.Thread(MyThre ad);
thread.Start();
}
void MyThread()
{
throw new ApplicationExce ption("My exception");
}
Why not?
Thanks as ever,
Aine
Comment