Are you a Windows 8 Application Developer? Are you building an app like audio/video player or game which needs to keep the display active when user is inactive in the system? Then you came to a right place where we will discuss how to keep the display on while idle.
This post details about the Windows 8/Windows RT API usage and code snippet to request the system to keep the display on. Continue reading to know more about it.
Microsoft exposes a sealed class named “DisplayRequest” present under the “Windows.System.Display” namespace, which provides APIs to interact with the display. It exposes two methods called “RequestActive()” and “RequestRelease()” which asks the Windows 8 or Windows RT system to keep the display on or off based on user’s activity.
When you call “RequestActive()”, this will make sure that the display remains ON when user is inactive in the system. It is mainly require when you are building a video application or a game application that needs the system display to keep ON all the time.
When you call “RequestRelease()” method, it releases the handle. After successful release, based on the system settings the OS will turn off the screen as and when require.
Code Implementation
As mentioned above, it is very easy to implement the same in your Windows 8/Windows RT applications. Just you need to create the instance of Windows.System.Display.DisplayRequest first and then call the appropriate APIs as mentioned in the below code snippet:
/// <summary>
/// Private instance of the Windows.System.Display.DisplayRequest
/// </summary>
private DisplayRequest m_displayRequest;
/// <summary>
/// This will request the system to activate the display to remain on while user is inactive
/// </summary>
private void KeepDisplayOn()
{
if (m_displayRequest == null)
{
// create the instance of the Windows.System.Display.DisplayRequest first
m_displayRequest = new DisplayRequest();
// request to activate the display ON. if successful, the screen will not
// turn off automatically due to user inactivity.
m_displayRequest.RequestActive();
}
}
/// <summary>
/// This will request the system to deactivate the display system to keep on all the time
/// </summary>
private void ReleaseDisplay()
{
if (m_displayRequest == null) { return; }
// deactivate the request to keep the display ON. it will automatically turn off
// the display based on settings configured in the system.
m_displayRequest.RequestRelease();
// finally set the display request handle to null
m_displayRequest = null;
}
Isn’t it too simple? The code says all the things here. Just go thru the above code to know the exact process. Still have queries? Let me know by leaving your query below and I will make sure to answer you as soon as possible. Please accept a delay if I am busy with my work.
Connect with me on Twitter and Facebook to get technical updates regularly. Also, you are most welcome to discuss with me on various technical issues. I am available in Google+ too. Subscribe to my G+ feed, RSS feed and email newsletter to get immediate update of my posts. Happy coding. Cheers.