I have a settings save method I call, but I tried unload, and lost focus the application will close out and not save before ever getting to either of those methods. When should I save application settings to keep this from happening?
Should I use a timer and save every 30 seconds, or what?
How often you save depends on your app. However, the key timings are:
Launchingis called when the app is first launched from the main screen andClosingis called when the user presses the back key to exit your app. Naturally, you’ll most likely want to save permanent data in theClosingevent.Activatedis called when the user has closed your app via theWindowsbutton and has gone back into it by pressing the back button. This doesn’t get called if the user launches the app for the first time.Likewise, the
Deactivatedevent is called when the user presses theWindowsbutton. Depending on your app, you’ll want to save transient data at this point so that when it’s restored, you can give the illusion that your app wasn’t closed at all. (Otherwise, for example, all textboxes will become empty even if the user entered data before pressing theWindowsbutton).Those are the main events, so you can design your app around that. One thing to remember is that if your save files are going to be big, and they take longer than
10 secondsto save after theclosingevent is called, your app will be terminated immediately, possible corrupting the save file. Therefore, for large saves files, you should plan ahead by saving incrementally (for example, after the user has made a change that should remain permanent).There’s no
one size fits allsolution to this as saving timings are highly dependant on the type of app being developed. Have a read of the Execution Model MSDN Page as it goes into more detail and provides code examples.