tags:

views:

320

answers:

3

Is it possible in .NET to ascertain whether my application is closing due to Windows being given a shutdown command (as opposed to any old application closing) in order to either write out some temporary cache files or even block the shutdown long enough to prompt for user input?

Whilst my current scope involves a Winform app and a windows service, I am interested in understanding this in a generic way if possible

+7  A: 

SystemEvents.SessionEnding looks like a good starting point for you. That article talks about the event sequence involved when a logout/shutdown is occurring.

Harper Shelby
+4  A: 

In general, you will want to handle the WM_QUERYENDSESSION Windows message. This will give your application a chance to do cleanup, or to block the shutdown if it's really necessary.

Greg Hewgill
+2  A: 

Handle the SessionEnded event on the Microsoft.Win32.SystemEvents type.

RoadWarrior