I have a static class that creates several worker threads in its constructor. If an exception occurs before the workers have been created my Application.ThreadException handler (used to shut the app down if an error not known to be recoverable occurs) triggers normally and everything is fine. Once the first worker thread has been created however in addition to the handler firing I get an "MYAPP has encountered a problem and needs to close. We are sorry for the inconvenience." dialog for MS error reporting.
In this specific instance I can reorder the code to create the threads last (after any resource initialization/access problems that could've triggered an exception) but that's no more than a bandaid to the problem and doesn't give me any information about what's actually going on.
Hopefully I've excised enough code from my app to show what I'm trying to do here.
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
Application.Run(theForm);
theForm.Dispose();
}
catch (Exception e)
{
//doing this to use the same handler here and for Application.ThreadException
ThreadExceptionEventArgs argsEx = new ThreadExceptionEventArgs(e);
FatalExceptionHandler(null, argsEx);
}
finally
{
MyStaticClass.KillThreads();
}
}
public static void FatalExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs ex)
{
Exception e = ex.Exception;
try
{
//lots of stuff to give more useful error messages for known problems
//and display them in a messagebox.
}
// if anything went wrong scraping the exception text for formatting, show the raw value.
catch
{
MessageBox.Show(e.Message);
return;
}
// after showing the MessageBox, close out the app.
finally
{
System.Environment.Exit(1);
}
}
}
class MyStaticClass
{
static MyStaticClass()
{
myThread = new Thread(new ThreadStart(SomeMethod));
//if this exception is thrown everything works normally
//Throw new Exception("KABOOM");
myThread.Start();
//if this exception is thrown a windows error reporting dialog appears
//along with the messagebox from program.FatalExcetion handlder
//Throw new Exception("KABOOM");
}
public void KillThreads()
{
//clean up the worker threads
}
}