Search code examples
c#.netshutdown

Saving state before shutting down using .NET


I'd like to save the state of my machine before shutdown (for machines that do not support hibernate).

Essentially, I'm trying to mimic the Windows Hibernate feature. When the machine its turned back on, it looks exactly like it did previous to being shut down.

Any ideas on using managed code to perform this task?

Currently using/considering Windows XP Service Pack 2.


Solution

  • For all applications running on your computer, this is simply not possible using pure managed code. In fact, even with unmanaged code you will have a hell of a time. I wouldn't say it's impossible but likely extremely difficult and time comsuming.

    Here are a few helpful resources to get you started:

    Arun Kishan on Windows Kernel
    http://www.dotnetrocks.com/default.aspx?ShowNum=434

    Core Dump
    http://en.wikipedia.org/wiki/Core_dump

    setcontext
    http://en.wikipedia.org/wiki/Setcontext

    Raymond Chen on "Hibernating" single processes
    http://blogs.msdn.com/oldnewthing/archive/2004/04/20/116749.aspx

    For your own application, your best bet is to isolate all of the state you would like to be able to restore into a set of serializable classes. Then, when your application is unloaded (or periodically), save this data to disk using XMLSerializer. When your application is loaded again, use the XMLSerializer again to rehydrate your classes holding the state of your application and use this information to return the user interface to the previous state. If you have complex user interfaces this could be a time consuming task.