I'm working on a bug where code is not always being executed before an application shuts down. The code was in a handler for the AppDomain.CurrentDomain.DomainUnload event.
I found a post by someone with the same problem who received this advice
"By the time the DomainUnload event happens for your default app domain, your code has stopped executing. You can probably do what you need to with the ProcessExit event on the default AppDomain."
This worked for me but I would like to know why. I haven't been able to find much on either of these events or on the differences between them. I'm also wondering if I need to subscribe to both or is the ProcessExit sufficient.
EDIT:
I wanted to add more information to make this a little more useful.
I left out that new threads were being created in their own AppDomain. Since I wanted this code to run not only when the parent process was done but also when each thread finished I needed to subscribe to the DomainUnload event to handle when each thread finished and also the ProcessExit event to catch when the parent process finished.
ProcessExit
should be sufficient.
The DomainUnload
event is designed to be handled by other AppDomains, not the AppDomain
being unloaded. As a result, if the handler is attached in the domain being unloaded, it might not run. The ProcessExit
event is designed to run when the process is going to exit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With