This is a follow up to my initial question and I would like to present my findings and ask for corrections, ideas and insights. My findings (or rather interpretations) come from people's answers to my previous question, reading MSDN .NET 3.5 documentation and debugging .NET 3.5 code. I hope this will be of value to someone who was wondering like me how to detect when an application terminates.
System.AppDomain.CurrentDomain.ProcessExit: raised when process exits, e.g. after the default
AppDomainand everything else was unloaded [Total execution time is limited to just 3 seconds!]. For WPF, use
System.Windows.Application.Exitinstead. For Windows Forms, run code after
Application.Run(...)in main method.
System.AppDomain.CurrentDomain.DomainUnload: raised when an
AppDomainother than default
AppDomainunloads, e.g. when running classes with unit testing frameworks (MbUnit with TestDriven.NET).
System.AppDomain.CurrentDomain.UnhandledException: (if handled in default
AppDomain:) raised for any unhandled exception in any thread, no matter what
AppDomainthe thread started in. This means, this can be used as the catch-all for all unhandled exceptions.
System.Windows.Application.Exit: raised when WPF application (i.e. the default
AppDomain) exits gracefully. Override
System.Windows.Application.OnExitto take advantage of it.
Finalizers (destructors in C#): run when garbage collector frees unmanaged resources. [Total execution time is limited!].
Order of events:
WPF application: graceful exit
WPF application: unhandled exception
MbUnit running inside TestDriven.NET: passed test (graceful exit)
MbUnit running inside TestDriven.NET: failed test (unhandled exceptions are handled by MbUnit)
- Are my interpretations/findings correct?
- Do you know of more details that I have left out? E.g. what is the total execution time for finalizers?
- Do you know of any other events / ideas that I be aware of?
- What events are there and what order do they get raised in other applications, e.g. Windows Forms, Web Service, ASP.NET web site, etc?
Prompted by ssg31415926's question/answer (this question is a bit reversed), there's also Application.SessionEnding which is called when the when the user logs off or shuts down. It is called before the Exit event.