Best way to save state (Windows Forms)
-
Every 30 seconds or so, I need my application to persist state, just in case the power goes off or something like that. What's the best method based upon this time frame? For example: - Save to an XML configuration file. - Save to registry. - Save to binary file (serialize object?) - Something better? Thank you. Sammy "A good friend, is like a good book: the inside is better than the cover..."
-
Every 30 seconds or so, I need my application to persist state, just in case the power goes off or something like that. What's the best method based upon this time frame? For example: - Save to an XML configuration file. - Save to registry. - Save to binary file (serialize object?) - Something better? Thank you. Sammy "A good friend, is like a good book: the inside is better than the cover..."
Don't save to the registry with .NET applications. It's not a good practice for many reasons, especially deployment issues. Other than that, save to whatever file format you want. Binary serialiation is faster than XML, but XML gives you the ability to easily customize the state while the program is not running. One other way - albeit more difficult - would be to implement a custom
BindingManagerBase
derivative that uses a file as a backing store and bind the properties of controls you want persisted. When they change, the binding manager is updated and your derivative class could save state immediately. This way, you don't have to poll and don't have to keep a timer, which is relatively inefficient since the state of your application might not have changed. See the documentation for theBindingManagerBase
andControl.DataBindings
in the .NET Framework SDK for more information.Microsoft MVP, Visual C# My Articles
-
Every 30 seconds or so, I need my application to persist state, just in case the power goes off or something like that. What's the best method based upon this time frame? For example: - Save to an XML configuration file. - Save to registry. - Save to binary file (serialize object?) - Something better? Thank you. Sammy "A good friend, is like a good book: the inside is better than the cover..."
Another consideration for your design should be: What happens if the power goes out WHILE the state is being saved? If your app is that critical, you might want to consider a UPS and monitor that for a power outage so your app can save it's state one last time before the power REALLY goes out. RageInTheMachine9532
-
Another consideration for your design should be: What happens if the power goes out WHILE the state is being saved? If your app is that critical, you might want to consider a UPS and monitor that for a power outage so your app can save it's state one last time before the power REALLY goes out. RageInTheMachine9532
Okay, it's not THAT critical :) Thanks! Sammy "A good friend, is like a good book: the inside is better than the cover..."