locked
What is the the stored session data in the tutorial supposed to do?

    Question

  • I'm referring to this tutorial  - "Part 2:Manage app lifecycle and state" (this one: msdn.microsoft.com/en-us/library/windows/apps/jj663503.aspx)

    This is how I understand it: The personalized greeting is stored as session data so that when you switch to another app (say alt+tab to Chrome or Visual Studio) and go back, the greeting will still be there. However, even at the start (Part 1), without the code that goes in the else part ( the one with "// TODO: This application has been reactivated from suspension."), the greeting is still there when I go to and fro between apps. I've also played around with the suspend and resume buttons in Visual Studio and I still don't see how session Data comes into play.

    One thing I did notice though is that the greeting is not loaded when the app is newly launched (say with F5/debug), since the session data is cleared every time the app is closed, is that correct?

    Wednesday, October 30, 2013 8:19 PM

Answers

  • The sessionState stores state information when the app suspends so that it is still available the next time the app runs.

    There are two tricky parts here:

    1. The app doesn't necessarily lose its memory when it gets suspended. The app will stop processing CPU immediately, but the memory won't be discarded until the OS needs it for another app. If you just switch away and switch back then the app will likely be in the same state even if it doesn't store it.
    2. Apps don't suspend when debugged. The suspend and resume buttons mimic suspension, but aren't exactly the same thing.

    As far as the session data being cleared if the app is closed, it depends on what you mean by closing the app. It will take several seconds after switching away from the app before it suspends, and if the app doesn't suspend then the sessionState isn't stored. This is called out in the Remarks in the sessionState documentation.

    --Rob

    Thursday, October 31, 2013 2:12 AM
    Owner

All replies

  • The sessionState stores state information when the app suspends so that it is still available the next time the app runs.

    There are two tricky parts here:

    1. The app doesn't necessarily lose its memory when it gets suspended. The app will stop processing CPU immediately, but the memory won't be discarded until the OS needs it for another app. If you just switch away and switch back then the app will likely be in the same state even if it doesn't store it.
    2. Apps don't suspend when debugged. The suspend and resume buttons mimic suspension, but aren't exactly the same thing.

    As far as the session data being cleared if the app is closed, it depends on what you mean by closing the app. It will take several seconds after switching away from the app before it suspends, and if the app doesn't suspend then the sessionState isn't stored. This is called out in the Remarks in the sessionState documentation.

    --Rob

    Thursday, October 31, 2013 2:12 AM
    Owner
  • Okay, thanks for that. Given what you said in 1.,  why then do we need to store data for the greetingOutput div?

    Also, on 2, does this mean that I have to do something else to see the effects of having saved session data?

    Thursday, October 31, 2013 4:53 AM
  • You need to store data because the memory might be freed. It's just not guaranteed, so a quick test may appear to work correctly without storing the data but might fail in the real world.

    If you want to see the full effects of suspension then you need to run the app outside of the debugger. To make sure the data was saved I'd let the app suspend and then log out and back in. That will ensure that the app closed out and didn't just remain in memory. Alternatively you could just leave it alone until it disappears in Task Manager. How long this takes will depend on how much memory your computer has and what else you do in the meantime.

    That said, for most cases the suspend and resume menu items should be sufficient.

    --Rob

    Thursday, October 31, 2013 5:06 AM
    Owner
  • The assumption is, without storing the session data, the greetingOutput would be empty on suspend and then resume, is that correct?
    Thursday, October 31, 2013 5:08 AM
  • No. If we resume then greetingOutput will still have the data since the app wasn't terminated and its memory freed. If after suspending the app gets terminated then on restart the greetingOutput will not have the unstored session data.

    Thursday, October 31, 2013 5:14 AM
    Owner
  • Okay, thanks for clearing that up.

    It seems that things got messy because of debugging. When I launch the app from VS with debug, the greeting remains blank. However, when I launch the app from the start menu (as you suggested), the greeting is restored. Why is this?

    Thursday, October 31, 2013 5:43 AM
  • Actually, the greeting is only stored when I look it up in the start menu manually. When I pinned it to the start menu, it had the same behavior when debugging; it had no greeting.
    Thursday, October 31, 2013 5:47 AM
  • Pinning vs. start menu is almost certainly coincidental rather than causal. If you don't save the state or if you terminate the app without letting it suspend then there wont be a greeting to restore. If suspend but don't terminate the app then the greeting won't need to be restored.

    Thursday, October 31, 2013 10:34 PM
    Owner
  • Okay, I got something. When I press alt+f4 and relaunch, there's a splash screen, and there's no greeting to restore (the app was terminated and the session ended). When I drag it from top to bottom, there's no splash screen when I relaunch it (go to Start and search for it), so the greeting's still there. The greeting being there is the effect of the session data rather than it being not freed from memory (as in the case of alt+tab) right?
    Friday, November 1, 2013 5:47 AM
  • If there is a splash screen then the app had to restart. The greeting will be pulled from the saved state.

    If there is no splash screen then the app is resuming. The greeting will still be in memory.

    Friday, November 1, 2013 9:49 PM
    Owner
  • By resume you mean from suspend right (or by "in memory" do you mean it is there as in the case of shifting between apps, or memory as in stored as session data)?
    Saturday, November 2, 2013 4:15 AM
  • Suspend stops the CPU for the process, but leaves it in memory. Eventually it will be terminated. If you switch back to the app (in any way) before it has been terminated then it will resume with its memory intact.

    --Rob

    Tuesday, November 5, 2013 11:34 PM
    Owner