36

I'm having a problem with suspending event on Windows Phone 8.1 using WinRT, it does not fire. I don't know why. This is my code:

/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
    InitializeComponent();

    Suspending += OnSuspending;
#if DEBUG
    this.displayRequest = new DisplayRequest();
#endif
}

/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">
/// The source of the suspend request.
/// </param>
/// <param name="e">
/// Details about the suspend request.
/// </param>
private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    deferral.Complete();
}

I set a breakpoint on the line var deferral = e.SuspendingOperation.GetDeferral(); and debugged it with Visual Studio. Then I pressed the start button on my phone and ran another app and waited about 10 seconds. OnSuspending is not running.

Any ideas?

AlSki
  • 6,868
  • 1
  • 26
  • 39
UltimaWeapon
  • 2,343
  • 18
  • 19

1 Answers1

65

Suspending event won't fire while you are debugging (but while running your App normal, it will fire just after you navigate away from the App) as it is said also at this blog:

...you will wait forever for these to trigger, even though your app switches back and forth to the screen! The reason is simple: while an app is being debugged, Windows will not suspend it.

Note that this may lead to some weird app behavior, when there is something wrong in Suspending event - for example if you pass some complex class in Frame.Navigate method and you use SuspensionManager. While debugging your app will work just fine (no suspension), but will crash without debug mode.

To test how your App behaves, you will have to invoke the Suspending manuallt, open (or set visible) Debug location toolbar in Visual Studio, there you will find a dropdown Lifecyce events, choose there Suspend, and then to return the App - Resume.

enter image description here

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Could you please provide any proof link? – CAMOBAP Nov 24 '14 at 12:20
  • 3
    @CAMOBAP A proof link to what? – Romasz Nov 24 '14 at 12:30
  • Proof link for the statement "Suspending event won't fire while you are debugging" (if it exists) – CAMOBAP Nov 24 '14 at 12:45
  • 7
    @CAMOBAP I'm not sure why you want me to show you the proof link - it wasn't hard to find so here it goes - [MSDN blog](http://blogs.msdn.com/b/mspfe/archive/2013/06/17/suspend-and-resume-in-winrt.aspx) -> 'How to debug process lifecycle events', it says: *If you want to debug the code from the previous sections by setting a breakpoint in each handler and hitting F5 to start the debugger in Visual Studio, you will wait forever for these to trigger, even though your app switches back and forth to the screen! The reason is simple: while an app is being debugged, Windows will not suspend it.* – Romasz Nov 24 '14 at 12:51
  • 4
    It's taken me hours to find this little golden nugget. TBF I didn't really know what I was looking for - the symptoms I had (and what I was Googling) were "windows phone application crashes but not in debug mode debugging" - perhaps leaving this comment will help Google to link to this great answer for other people Googling the same thing. This answer doesn't resolve the issue but it helps you to identify the issue. – Percy Apr 13 '15 at 10:58
  • @Rick Thank you for the comment, I've edited a little the answer, so maybe it will help to spare some hours of other people. It seems to be quite common problem, especially connected with navigation. – Romasz Apr 13 '15 at 11:09