Answered by:
Detect system sleep/hibernate( suspend/resume ) in metro app

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
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, August 8, 2012 12:44 PM
- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Tuesday, August 14, 2012 11:05 PM
Wednesday, August 8, 2012 5:48 AMModerator -
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)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Thursday, August 9, 2012 5:29 PM
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Monday, August 13, 2012 5:26 PM
Thursday, August 9, 2012 5:29 PMModerator
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
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, August 8, 2012 12:44 PM
- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Tuesday, August 14, 2012 11:05 PM
Wednesday, August 8, 2012 5:48 AMModerator -
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)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Thursday, August 9, 2012 5:29 PM
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Monday, August 13, 2012 5:26 PM
Thursday, August 9, 2012 5:29 PMModerator -
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)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Thursday, April 4, 2013 3:55 PM
Thursday, April 4, 2013 3:55 PMModerator -
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 PMModerator -
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 PMModerator -
- 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 25, 2013 7:19 AM -
Hi, an update on this would be great. Thank you.Thursday, August 8, 2013 12:56 AM