I'm working on a project that is implemented using plain threads
. Most of the expected exceptions in the application are handled, however, there are cases where one of the threads throws an unexpected exception and the application just crashes (the application is both I/O
and Client-Server
based so it's practically impossible to handle all the exceptions).
To fix this, I'm trying to define a global UnhandledExceptionHandler
so that the application displays a friendly message instead of crashing. This is what I tried:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
// The rest of the startup logic goes here
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Utilities.DisplayUnhandledException((Exception)e.ExceptionObject);
}
}
This doesn't work though. The CurrentDomain_UnhandledException
is never called. Unfortunately, I can't change the structure of the application, which means I can't use the Task Parallel Library. I can't figure out why this doesn't work. Is there any other way to handle exceptions thrown in threads? Any help is appreciated.