title: ProcessExit vs DomainUnload
body: <p>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.</p>
  
  <p>I found a post by someone with the same problem who received this advice </p>
  
  <p>"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."</p>
  
  <p>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.</p>
  
  <p>EDIT:</p>
  
  <p>I wanted to add more information to make this a little more useful.</p>
  
  <p>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.</p>