Fabian's Mix

Mixins, .NET, and more

Archive for the ‘user experience’ Category

How come Word is back after Setup.exe restarts my PC?

without comments

Some time ago, when I started using Windows Vista and didn’t have the Windows Update Settings tuned to never restart without my consent, it happened that I was working in Word (and maybe ignoring some Windows Update process in the background), when suddenly my PC shut down. “What the heck?” was my first reaction to this (as I usually try to avoid using evil curse(d) words), and “I didn’t save” was the second one. (The third one was, “That damned program didn’t even ask me whether I wanted to save!”) But strangely, some angel heard my muttering and, after I had logged in again, automatically brought back Word with all my unsaved data.

If you’re using Vista or Server 2008, you might also have noticed that some programs, for example Microsoft Word 2007, Internet Explorer 7, and Visual Studio 2008, silently shut down when an installer needs to restart the system, and get restarted with their sessions restored when the system is up again. That’s a very nice user interface feature, I think, so I decided to research how to do it. And it turned out it wasn’t hard at all, as Daniel Moth explains: Vista (and thus Server 2008) contains a new Restart Manager, which enables this feature and is used automatically by Windows Installer 4.

To enable your application to restore its sessions, you simply listen for WM_QUERYENDSESSION with an lParam of ENDSESSION_CLOSEAPP (save your state there) and register your application for restart using RegisterApplicationRestart (which is part of the automatic recovery functions, see the MSDN for the P/invoke signatures). If you have a console application, you can also use a CTRL_C_EVENT handler (or the Console.CancelKeyPress event) to save your state, but in this case, you have to call RegisterApplicationRestart before you get that notification. See the MDSN for more comprehensive guidelines.

And that’s it, your application is ready for a clean Windows Update experience.

Written by Fabian

January 5th, 2009 at 8:58 am