views:

1872

answers:

5

I want to be able to intercept the shutdown event in C# for Windows Vista. Due to the advanced security features with Vista, any applications that are running after the shutdown command is called are halted and displayed in a list, prompting the user to do something with them.

Does anybody know how to overcome this and what events I need to be using in Vista.

Thanks.

A: 

System.Environment.HasShutdownStarted

Eduardo Campañó
This is not actually a system shutdown event for vista - it's a property set to true on any platform when your appdomain is shutting down.
Philip Rieck
+1 for you, but if HAdes wanted the application to shutdown when windows is shutting down not to appear in the list this should work.
Eduardo Campañó
+4  A: 

You can use WPF's application object and subscribe to its SessionEnding event. You can then look at the SessionEndingCancelEventArgs.ReasonSessionEnding enumeration to determine exactly why the session is ending (LogOff or Shutdown).

Ken Wootton
+2  A: 

What you may want to look at is here - Application Shutdown Changes in Windows Vista. Basically, for what you want, it all revolves around WM_QUERYENDSESSION.

Note that this is exposed in the .net framework - instead you will need to use native functions (p/invoke) and hook the wndproc in your code to respond to the windows message.

For an example (showing a reason to not shutdown), you can see Windows Vista - ShutdownBlockReasonCreate in C#.

Philip Rieck
A: 

Use the event

Application.SessionEnding for WPF.

mattlant
A: 

The SessionEnding / SessionEnded events on Microsoft.Win32.SystemEvents might be what you are looking for.

csgero