Windows 8 has great power savings features that dim the monitor or turn it off completely based on whether or not the user is actively interacting with the device. This works great for most applications, but if you happen to be developing an application such as a movie player or book reader, the user can go for long periods of time without actually interacting with the device. For media playback and similar applications, this power saving feature presents a problem. This blog takes a look at the code involved in preventing the device display from turning off.
Windows 8 Store Apps
To keep the display enabled in a Windows 8 Store App regardless of the lack of user activity, the class to use is
Windows.System.Display.DisplayRequest. There are only two methods available in a
RequestActive is called to keep the display on,
RequestRelease is used when the app no longer needs to keep the display on.
To prevent the OS from turning the display off, create a new
DisplayRequest object and call
RequestActive(). The following example is written in C#:
MyDisplayRequest = new DisplayRequest();
To allow the OS to control the display:
Here is the same example given in C++:
Windows::System::Display::DisplayRequest ^mDisplayRequest = ref new DisplayRequest();
The developer is responsible for managing a call to the release method for each request. If the application is no longer in the foreground, the operating system automatically deactivates any outstanding requests to keep the display on and will reactivate them if the app is returned to the foreground. An application in Fill or Snap mode is considered to be in the foreground.
Additional reference for doing this in C#/VB/C++:How to keep the display on during audio/video playback (Windows Store apps using C#/VB/C++ and XAML)
Windows 8 Desktop Apps
For the same type of behavior in a Windows 8 Desktop application, the function
SetThreadExecutionState (http://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx) accomplishes the task.
To keep the display on, use the following flags in a call to
SetThreadExecutionState (C++ Example):
You are still required to clear the flag when your application no longer needs to keep the display from sleeping:
If you don't specify the
ES_CONTINUOUS flag, the call will only reset the display timer much like moving your mouse. In addition, you can use the
ES_AWAYMODE_REQUIRED flag to tell the operating system that your application needs to perform some critical background processing. Using this flag will prevent the computer from entering a true sleep.
To perform the same task in C# a little extra work is required since there is no native .Net library that exposes this functionality. Once the
SetThreadExecutionState is defined, the code to control the display is exactly the same.
const uint ES_CONTINUOUS = 0x80000000;
const uint ES_DISPLAY_REQUIRED = 0x00000002;
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "SetThreadExecutionState")]
public static extern uint SetThreadExecutionState(uint esFlags);
Prevent the OS from turning the display off:
SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);
Allow the OS to control the display:
For further information about System Sleep in desktop apps: http://msdn.microsoft.com/en-us/library/windows/desktop/aa373233(v=vs.85).aspx
If your application does not make use of one of the methods outlined above, the users could resort to explicitly adjusting the power settings available in Windows 8 to keep the monitor from shutting off. Changing this system wide power setting for a single application is not ideal and could have a greater impact on the systems power consumption. You can provide the best user experience by controlling the display from within the app and keeping the display on only when necessary.
*Other names and brands may be claimed as the property of others.
**This sample source code is released under the Intel OBL Sample Source Code License (MS-LPL Compatible) and Microsoft Limited Public License. Copyright© 2013 Intel Corporation. All rights reserved.