How to keep the Win8 Display ON during audio/video playback?


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.


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

3 comments

  1. thanks for post i goona use it....

    ReplyDelete
  2. Thanks for this usefull information. But where exactly i am calling this two functions, is any default evennt like Unload.

    ReplyDelete


 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top