title: How to detect Windows shutdown or logoff
body: <p>I need to detect when Windows is shutdown (or restarted) or when the user is logging off. I need to properly close the application before the application is closed.
  I noticed that no exit application event is raised when Windows is closing day.</p>
  
  <p>I read the post <a href="https://stackoverflow.com/questions/4617538/is-there-a-way-in-c-to-detect-a-windows-shutdown-logoff-and-cancel-that-action">Is there a way in c# to detect a Windows shutdown/logoff and cancel that action (after asking the user)</a></p>
  
  <p>but I'm not sure of where I should perform the operations before closing.
  Thanks.</p>