13

I have written an app that performs some lengthy operations, such as web requests, in a background thread. My problem is that after a while the automatic screen lock turns the screen off and my operations are aborted.

Is there a way to prevent the screen to be automatically turned off during these operations? Or is it in some way possible to keep running while screen is turned off?

I know there are ways to prevent the screen to turn off while debugging, but i need this behavior in the hands of the end user. Therefore I can not rely on some setting being set on the phone, but rather some programmatic solution.

PKeno
  • 2,694
  • 7
  • 20
  • 37
  • possible duplicate of [How to stop Windows Phone 7 from locking screen?](http://stackoverflow.com/questions/5518715/how-to-stop-windows-phone-7-from-locking-screen) – MarcinJuraszek Apr 11 '13 at 07:27
  • @MarcinJuraszek That question addresses another problem while testing and debugging. My problem arises when the end user is using the app. Will update question to be more clear. – PKeno Apr 11 '13 at 07:30
  • possible duplicate of [Can I prevent screen timeout on Windows Phone 7?](http://stackoverflow.com/questions/3920072/can-i-prevent-screen-timeout-on-windows-phone-7) – Paul Annetts Apr 11 '13 at 08:53

2 Answers2

20

The screen can be forced to stay on using the UserIdleDetectionMode property of the current PhoneApplicationService.

To disable automatic screen lock:

PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;

To enable it again:

PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;

More information can be found on MSDN

PKeno
  • 2,694
  • 7
  • 20
  • 37
  • 4
    There are some extra things Microsoft recommends that you do when you have this kind of behavior. They are listed here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff941090%28v=vs.105%29.aspx – Owen Johnson Jul 27 '13 at 04:45
8

I know this question is about Windows Phone 8, but I had a hard time figuring out the way for Windows Phone 8.1 (Universal XAML Apps). Use:

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

Apps that show video or run for extended periods without user input can request that the display remain on by calling DisplayRequest::RequestActive. When a display request is activated, the device's display remains on while the app is visible. When the user moves the app out of the foreground, the system deactivates the app's display requests and reactivates them when the app returns to the foreground.

See: http://msdn.microsoft.com/en-us/library/windows/apps/br241816.aspx

stefan.s
  • 3,489
  • 2
  • 30
  • 44