How to gracefully handle computer shutdown from WPF application

From Yggenyk
Revision as of 14:39, 25 April 2014 by WikiSysop (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

WPF application lifetime.png

Application Events

When you start the application

Startup -> Activated

When you shut the application down normally (i.e. close main window)

Deactivated -> Exit

If user is logging out of or shutting down Windows

Deactivated -> SessionEnding

Application gets focus

Activated

Application loses focus

Deactivated

AppDomain Events

When an exception is not caught

UnhandledException

(DispatcherUnhandledException is raised by an Application for each exception that is unhandled by code running on the main UI thread, but if you can safely ignore that and wait for UnhandledException is raised by AppDomain)


How to simulate Windows shutdown for debugging?

There is a tool named Restart Manager (rmtool.exe) in the Microsoft's Logo Testing Tools for Windows, which can be used to send shutdown and restart messages to a process. Logo testing tools can be downloaded here:

Then you can simulate shutdown for your process:

rmtool.exe -p [PID] -S
where [PID] is the process ID. According to the Vista Logo Certification Test Cases document,

Restart Manager shutdown messages are:

a) WM_QUERYENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): GUI applications must respond (TRUE) immediately to prepare for a restart.

b) WM_ENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): The application must shutdown within 5 seconds (20 seconds for services).

c) CTRL_SHUTDOWN_EVENT: Console applications must shutdown immediately.


id=siteTree