locked
After Suspending event is raised, my UI thread gets a few more cycles working elsewhere in my app ... is that expected? RRS feed

  • Question

  • Hi,

    My app continues to run for a short time after the App.Suspending event gets raised. This makes it tricky to save state since in my suspending handler, while I'm trying to save state, the app is still running and changing that state.

    I have code similar to the following (the code here is simplified from my real app). This code runs on the UI Thread as does the Suspending event handler as far as I can tell - both threads have the same ManagedThreadId.

    var files = new StorageFile[100];
    var state = new bool[100]; // needs to be saved to disk on suspend
    // ...
    for (int i = 0; i < 100; i++) // loop runs 3 more iterations after Suspending event raises
    {
      await files[i].RenameAsync(..., ...);
      state[i] = true; // never reached on final loop iteration after Suspend
    }
    

    Suppose I'm in the middle of this loop and the Suspending event gets raised. My Suspending handler tries to write the application state to disk (represented by the array of bool above). However, the loop runs for 3 further iterations which changes the session state I'm trying to save.

    I can work around this by exiting the loop when Suspending is raised and saving a bit more state for my operations, but I'm still curious about a few things:

    1. Will the final RenameAsync complete even if my app gets suspended? From my testing, I think the answer is 'yes' but that could just be because it happens fast enough to complete before my app loses all its CPU cycles.

    2. Is what I'm seeing expected behavior? I.e. should the app continue to get CPU time *outside* of the Suspending handler *after* the Suspending event gets raised? Maybe this is why it's not named 'Suspended'?

    Thanks!

    Tuesday, October 8, 2013 8:54 PM

Answers

All replies

  • The OS will bring down the app after raising the Suspending Event.  You are just executing within that window so that is why it appears to let you finish.  Your logic in the app should see if you are in a suspending state and stop processing.  Have you considered saving state as the app runs instead of trying to do it all at once? 

    Yes, the app needs to continue to run or you would not be able to execute code in this handler.  The OS does not suspend all your app threads.


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, October 9, 2013 12:59 PM
    Moderator
  • Your logic in the app should see if you are in a suspending state and stop processing. 

    Hi Jeff,

    Thanks for the confirmation that this is expected! Yes, I do check for Suspending and stop processing.

    I'm still curious if you know whether the RenameAsync should finish in the case where my app gets suspended immediately after the call? I.e. is it just luck that the call runs fast enough to finish before my app loses all its CPU time? Or does Windows actually finish the rename for me even if my app becomes fully suspended?

    I will save enough state that I can test if that last renamed finished or not so this is more of a theoretical question :-)

    Thanks again,

    Mark

    Wednesday, October 9, 2013 1:32 PM
  • Hi Mark,

    After a certain time the OS will just terminate the thread.  You are getting lucky right now :-) !

    Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, October 9, 2013 1:37 PM
    Moderator
  • Excellent, thanks again for the confirmation!
    Wednesday, October 9, 2013 1:46 PM