locked
How to check the ApplicationExecutionState on suspending?

    Question

  • I am facing an issue which I am not sure what to do next. Maybe I am missing something simple.

    I am creating a Windows app and in my App.Xaml.cs I have created my own OnSuspending method. Now, I know that as per the App Model lifecycle, the state can go from running to suspended when minimized. What I want to do is to differentiate between minimizing/suspending and terminating.

    If it's minimized, then I don't want something to happen. But if it's closed by user/Terminated, then I want a to perform a different function. (e.g. say I want to perform a survey)

    This is how I am trying to code -

    private void OnSuspending(object sender, SuspendingEventArgs e)
    {
       var deferral = e.SuspendingOperation.GetDeferral();
       ApplicationView.TerminateAppOnFinalViewClose = true;
      
       // if ApplicationExecutionState.ClosedByUser or AppicationExecutionState.Terminated
       // foobar

       deferral.Complete();
    }

    The issue that I am facing is that from SuspendingEventArgs, I am not getting what to use to compare the application state.

    Anyone knows how to solve this?

    Thursday, April 02, 2015 7:12 PM

Answers

  • In the case of minimize, the suspend does not happen right away, it takes sometime. Before the suspension the visibility of the application changes. Maybe you can make use of the Window.VisibilityCahnged event to get ready for the impending suspension.

    Can Bilgin
    Blog Samples CompuSight

    Saturday, April 04, 2015 5:56 AM
  • Hi Nemo666,

    See Application lifecycle for different behaviors on App suspend and App visibility.

    Basically Window.VisibilityChanged event for App visibility changed event and Application.Suspending event for App suspend.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 22, 2015 1:34 PM
    Moderator

All replies

  • An alternative solution: handling HardwareButtons.BackPressed event:

    public MyPage() { InitializeComponent(); #if WINDOWS_PHONE_APP Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtonsBackPressed; #endif } #if WINDOWS_PHONE_APP private void HardwareButtonsBackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) { e.Handled = true;

    //Do something Frame.GoBack(); }


    Best Regards,
    Please remember to mark the replies as answers if they help


    • Edited by IssueKiller Friday, April 03, 2015 10:06 AM
    Friday, April 03, 2015 10:06 AM
  • Hmm .. That's true for phone apps. Windows have this concept of "Universal apps" which will work for phone and desktop/surface. I am curious how to solve the desktop issue? There's no BackPressed there. The state goes from running -> Suspend
    Friday, April 03, 2015 7:56 PM
  • In the case of minimize, the suspend does not happen right away, it takes sometime. Before the suspension the visibility of the application changes. Maybe you can make use of the Window.VisibilityCahnged event to get ready for the impending suspension.

    Can Bilgin
    Blog Samples CompuSight

    Saturday, April 04, 2015 5:56 AM
  • Hi Nemo666,

    See Application lifecycle for different behaviors on App suspend and App visibility.

    Basically Window.VisibilityChanged event for App visibility changed event and Application.Suspending event for App suspend.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 22, 2015 1:34 PM
    Moderator