locked
Detect system sleep/hibernate( suspend/resume ) in metro app RRS feed

  • Question

  • Hi All,

    I check all answers about this topic, but nothing help me.

    Because of that I want to be very clear in next steps:

    1. Create new JavaScript Blank Solution in Visual Studio Express.

    2. Add following code, before app.start() function :

        app.onloaded = function (e) {

           var messageDialog;

            Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", function () {
                 messageDialog = new Windows.UI.Popups.MessageDialog("suspending");
                 messageDialog.showAsync();

            }, false);
            Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function () {
                 messageDialog = new Windows.UI.Popups.MessageDialog("resuming");
                 messageDialog.showAsync();
            }, false);
        }

     3. Launch App in Debug Mode.

     4. Click 'Suspend', 'Resume' in tool bar of Debugging( you need activate it before ) and you can see all message box.

      Look like everything fine, BUT!!!

     5. Just launch  your app, in release mode. And after this close your laptop(or Lock your machine). Leave for few sec. and

    open your laptop again. Nothing happen !!!

    It's mean in Visual Studio this routine work in real life doesn't, so my question is how I need subscribe to event Hibernate/Sleep

    of Windows in Metro Style App ?

    Thanks

    - Rost


    ;-)Rost

    Monday, August 6, 2012 10:43 PM

Answers

  • Hi Rost,

    The Suspending and Resuming events occur when the app suspends and resumes. This is different from when the system hibernates. There isn't a direct event for hibernate/sleep: this should be fairly transparent to the app, but there are events for other consequences of the sleep. For example, the app's window will be activated after the user logs back in, so the app will receive an Activated event.

    Note that the app doesn't get suspended at all when debugging: this is why you need to use Visual Studio's Suspend and Resume buttons to mimic suspending and resuming. The app won't get these events under the debugger when the user switches to other apps. This doesn't affect the sleep scenario, but even if the suspend and resume events would fire then, they wouldn't do so when the app ran under the debugger.

    --Rob

    Wednesday, August 8, 2012 5:48 AM
    Moderator
  • Rost,

    When you return from hibernation the system restores the running state of the whole system.  You do not need to (nor would you want to) detect this condition because system wide, it as if nothing had happened.

    Think of it this way, if you had notepad open and were typing some text, then the system hibernates or sleeps... Does notepad subscribe to any event or worry about the system state?  No, it does not.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, August 9, 2012 5:29 PM
    Moderator

All replies

  • Hi

    I can't repro your problem.

    When I click Suspend it will return to start menu, No Message Dialog popup, after that I click resuming, it will throw JavaScript runtime error: Access is denied.

    I think that right, because you can't popup a message Dialog when the app is suspend.

    So I command this two line in suspending, it works well.

    messageDialog = new Windows.UI.Popups.MessageDialog("suspending");
                  messageDialog.showAsync();
     

    And the SDK example provide a better way to use WRL handle suspending and resuming.

    Please check this: App activated, resume, and suspend using the WRL  sample

    Also you can use WInJS: App activate and suspend using WinJS sample

     

    Tuesday, August 7, 2012 6:09 AM
  • Hi Dino, thank you for response, and your time.

    Again, problem is NOT of suspending or resuming Metro App.

    Problem is HIBERNATE or SLEEP mode.

    Try, run app and close your laptop and open after few sec, you will not get ANY event( 'resume' in this case ).

    So my question is : If resume and suspend technique doesn't work for Hibernate/Sleep mode for Metro App,

    when Metro App front of User. What is technique will work in this case?


    ;-)Rost

    Tuesday, August 7, 2012 7:10 PM
  • Hi Rost,

    The Suspending and Resuming events occur when the app suspends and resumes. This is different from when the system hibernates. There isn't a direct event for hibernate/sleep: this should be fairly transparent to the app, but there are events for other consequences of the sleep. For example, the app's window will be activated after the user logs back in, so the app will receive an Activated event.

    Note that the app doesn't get suspended at all when debugging: this is why you need to use Visual Studio's Suspend and Resume buttons to mimic suspending and resuming. The app won't get these events under the debugger when the user switches to other apps. This doesn't affect the sleep scenario, but even if the suspend and resume events would fire then, they wouldn't do so when the app ran under the debugger.

    --Rob

    Wednesday, August 8, 2012 5:48 AM
    Moderator
  • Thanks, Rob.

    If I understand you correctly.

    When I open my laptop again, Metro App receive event activate

    and debugging in Visual Studio can't help me in this case.

    Is it correct ?


    ;-)Rost

    Wednesday, August 8, 2012 6:58 PM
  • Rost,

    When you return from hibernation the system restores the running state of the whole system.  You do not need to (nor would you want to) detect this condition because system wide, it as if nothing had happened.

    Think of it this way, if you had notepad open and were typing some text, then the system hibernates or sleeps... Does notepad subscribe to any event or worry about the system state?  No, it does not.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, August 9, 2012 5:29 PM
    Moderator
  • Thanks Jeff, nice example, help me a lot :)

    ;-)Rost

    Monday, August 13, 2012 5:16 PM
  • Notepad is probably the worst example you could give, at least when considering useful reasons for wanting to know when the system is going to sleep/shutting down. What if my app communicates with some web service that needs to know that the system is going to be going offline (sleep, etc)? In such a case, some kind of system event for sleep or shutdown would be great, but that probably won't happen.

    Thursday, April 4, 2013 3:40 PM
  • Again,

    You don't need to know why your app is being suspended.  You will get the suspend no matter what the reason and you should react appropriately.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, April 4, 2013 3:55 PM
    Moderator
  • The suspend event is not fired when the system goes to sleep or shuts down.

    Thursday, April 4, 2013 7:45 PM
  • It should as long as it is a release version of the app and not running under the debugger.

    If you have a solid repro of this not being the case, please post it!

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, April 4, 2013 7:54 PM
    Moderator
  • I was just talking to some co-workers about this.  I may be smoking my socks!  I will try this out tomorrow, no reason to post a repro, and I will update this post.


    Jeff Sanders (MSFT)

    Thursday, April 4, 2013 8:15 PM
    Moderator
    • I was just talking to some co-workers about this.  I may be smoking my socks!  I will try this out tomorrow, no reason to post a repro, and I will update this post.
    • Jeff Sanders (MSFT)


    dear jeff , I do face the same issue. I am using an persistent web connection in my app, which get disconnected when the tab goes to sleep state and no Event being triggered once the device resumes form sleep mode. Any update on this?
    Thursday, April 25, 2013 7:19 AM
  • Hi, an update on this would be great. Thank you.
    Thursday, August 8, 2013 12:56 AM