I would like to execute a function when the running application terminated via normal close way (right top X) or un expected error happened and software terminated.
How can i do this at c# 4.5 WPF application
Thank you
In your App.xaml.cs
-
OnStartUp
method and hook UnhandledException
event of
Current AppDomain
, it will get called whenever application was
about to close because of some unhandled exception.OnExit
method for normal close of application.Create CleanUp
method and call the method from above two methods.
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
private void CleanUp()
{
// Your CleanUp code goes here.
}
protected override void OnExit(ExitEventArgs e)
{
CleanUp();
base.OnExit(e);
}
void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
CleanUp();
}