5
votes

I have a little problem with saving my state to localsettings. Everything is ok except the situation when someone close my application using alt + f4 and open it before 10 seconds elapsed(after 10 seconds application is in state suspending and data is saved). (Technology xaml/c#)

I save my data in event OnSuspending.

I load my data in event OnLaunched like this:

if (args.PreviousExecutionState == ApplicationExecutionState.Terminated ||
args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
{
  // save data
}

How to handle this situation ? I know i can save my state every time it is changed but i think it is not good idea in my application. Thanks for help !

2

2 Answers

2
votes

When you close and launch your application before 10 seconds elapsed another instance of it is created and the previous one does not run OnSuspending event (it is strange because it means that asynchronous operations like this event can end or never start without warning us). I think that this is annoying but why would your user do something like that? Most of the times the user "restart" your application because it crashed or he is stuck and can't go back to the main page. You should try to prevent those scenarios and such think rarely will happen.

However, this can also happen because the user forgot to do something and want to start the app again. To prevent lost user data I save the most important data whenever I get the chance and save the rest only OnSuspending method. You need to think about what data will upset your users when lost.

I think Microsoft should get a better way of saving application state. I searched a lot about this problem and didn't found an explanation so for now I will continue to do what I said above. I hope this question you made can help and clarify me about this, in my opinion, strange case.

0
votes
onLaunched :
    CoreWindow.GetForCurrentThread().Activated += App_Activated;
and event:  
void App_Activated(CoreWindow sender, WindowActivatedEventArgs args)
        {



               if (args.WindowActivationState == CoreWindowActivationState.Deactivated)
               {
               //save Data
               }
       }

When you load data, remove: if (args.PreviousExecutionState == ApplicationExecutionState.Terminated || args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)`. It works !