public static void ApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
DialogResult result = DialogResult.Abort;
try
{
string errMsg =
"Message: " + e.Exception.Message +
Environment.NewLine +
Environment.NewLine +
"StackTrace: " + e.Exception.StackTrace +
Environment.NewLine +
Environment.NewLine +
"Source: " + e.Exception.Source +
Environment.NewLine +
Environment.NewLine +
"GetType: " + e.Exception.GetType().ToString();
// Since we can't prevent the app from terminating, log this to the event log.
if (!EventLog.SourceExists("ThreadException"))
{
EventLog.CreateEventSource("ThreadException", "Application");
}
Clipboard.SetDataObject(errMsg, true);
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "ThreadException";
myLog.WriteEntry(errMsg);
result = MessageBox.Show(errMsg, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
if (result == DialogResult.Abort)
{
Application.Exit();
}
}
}