Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear all
i want to make a failsafe program , now i have a program which makes many measurements and it contains many threads , now in case i can't use an UPS , i want my program to restore its last state after a sudden power off as if no power off was happened umm something like Windows hibernate

is it serialization or there is more easier robust solutions


thanks bye
Posted

If you aren't using a UPS, then a catastrophic power failure is a real problem. The only way you can "pick up where you left off", so to speak, is to save your application state every x number of seconds or so. How often you can do this will depend on the amount of data you need to save down and the impact this will have on your application performance. The issue with your thinking here may be a misunderstanding of what Windows hibernate actually does. Windows hibernate does not turn off the computer, it simply reduces the resources being used, but memory and such is maintained which is why everything is where you left it when you return from hibernation. A full power down is completely different.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Jan-12 20:58pm    
Agree, a 5.
--SA
That is not easy, at all.

Windows hibernate is different to what you are trying to do, it saves the current memory content to the HDD and reloads it when it wakes up - as it starts again at the exact point at which it left off.

You can't do that. Saving status like that needs power - and lots of it - as it has to run the HDD and write in0formation away. When the power fails, that is absolutely the last time you want to be writing to the disk, as it is quite possible that the act of writing away will corrupt the rest of the disk. That is why modern HDD's have a power fail detector, and stop writing and park the heads in a safe location when it occurs - to prevent the heads landing on the disk surface and physically damaging it.

You can do something similar - many programs do - by saving your app's status at regular intervals so that you can restore to a almost-up-to-date position in the event of a problem. Two things to watch for though:
1) Don't save too often. That will just keep the HDD access going, and worry people, as well as potential;;ly shortening tyhe life of the HDD.
2) Keep several copies, and make your you put good error checking in them, so that if the power fails while you are writing, you can detect it and use the previous version instead.

I would buy a UPS and be done with it, myself!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Jan-12 20:54pm    
My 5.
--SA
As additional information (to have a guidance on capturing your application state) you may have a look at the http://en.wikipedia.org/wiki/Memento_pattern[^].
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Jan-12 20:57pm    
Yes, this is not a trivial suggestion. Only, if you don't mind, I found your link a bit off and fixed it. Now it really points to this design pattern. But -- damn! -- by doing so I did not leave myself a chance to vote other than 5, so I voted 5. :-)
--SA
CPallini 18-Jan-12 3:11am    
Thank you both for fixing (of course I don't mind) and voting.
Just my two cents, since PC's are not designed to be fail-safe, and neither is Windows for that matter, there's no way to do what you want. Your app can only be as failsafe as the hardware and O/S it's running on.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900