I'm attempting to execute code on exit in a windows form application. In my Main() function, I've placed the following code:
Thread.GetDomain().UnhandledException +=
(sender, eventArgs) => Exiting((Exception)eventArgs.ExceptionObject);
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => Exiting(null);
Application.ApplicationExit += (sender, eventArgs) => Exiting(null);
This is what the Exiting() function looks like:
private static void Exiting(Exception exception)
{
if (exception == null)
{
Console.WriteLine("normal proc exit");
}
else
{
Console.WriteLine("unhandled exception: " + exception.GetType().Name);
}
}
However, it hasn't written the expected console line even once while debugging. What is the proper way to do this?