简体   繁体   中英

In UWP app (Windows 10), how can I prevent Windows OS from entering Sleep mode?

I am developing UWP app (Windows 10) that record user voice through microphone. I need to prevent OS going in to sleep mode while the recording is in progress.

I already used below code to prevent screen lock.

var displayRequest = new Windows.System.Display.DisplayRequest();
displayRequest.RequestActive(); 

But it only works on mobile phone, tables. Desktop PC stills going on sleep mode.

Make sure the DisplayRequest object has a global scope. If you only define it within the function then it goes out of scope and gets released and the computer will go to sleep.

Hold on to the global object for as long as you need, and then call RequestRelease() on it when your done.

While recording is in progress, your app should also be in the foreground otherwise it will be considered inactive and screen will be locked even after executing displayRequest.RequestActive(); .

An app is considered to be inactive (not running in the foreground) when:

  • Tablet mode: If your app is not focused then it is considered inactive
  • Desktop mode: If your app is minimized then it is considered inactive

For more details on this topic you can refer this blog post by windows app team.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM