[Windows 8] Prevent display to turn off when users does not interact with your application

December 26th, 2013 | Posted by Tom in .NET | Article | Windows 8 | WinRT | Read: 1,076

Today, we are going to see a small but useful tip for those who want to prevent a screen to turn off while there is no user activity.

This might be useful, for example, if there are some videos playing on your app: while the video are playing, the user does not interact with the app thus, depending to your power settings, the screensaver can start or the screen can even turn off!

To prevent this case, Microsoft offers the DisplayRequest API. The usage is really simple:

private DisplayRequest _displayRequest;

public void PreventDisplayToTurnOff()
{
    if(_displayRequest == null)
        _displayRequest = new DisplayRequest();

    _displayRequest.RequestActive();
}

public void AllowDisplayToTurnOff()
{
    if (_displayRequest == null)
        _displayRequest = new DisplayRequest();

    _displayRequest.RequestRelease();
}

Even if the usage is really simple, be careful of the following:

  • Use DisplayRequest API only when it’s necessary. Indeed, preventing the screen to start screensaver or to turn off impact battery life!
  • Don’t forget to release each request as soon as possible

Here is a simple helper that, I hope, will help you (this one might not be totally proper but, at least, it works on my case :)):

public class DisplayRequestHelper : IDisposable
{
    private bool _requestActivated;
    private DisplayRequest _displayRequest;

    public void PreventDisplayToTurnOff()
    {
        if (!_requestActivated)
        {
            if (_displayRequest == null)
                _displayRequest = new DisplayRequest();

            _requestActivated = true;
            _displayRequest.RequestActive();
        }
        else
        {
            throw new InvalidOperationException("An active request is already pending.");
        }
    }

    public void AllowDisplayToTurnOff()
    {
        if (_requestActivated)
        {
            if (_displayRequest == null)
                _displayRequest = new DisplayRequest();

            _requestActivated = false;
            _displayRequest.RequestRelease();
        }
        else
        {
            throw new InvalidOperationException("No active request found to be released.");
        }
    }

    public void Dispose()
    {
        try
        {
            if (_displayRequest != null)
            {
                _displayRequest.RequestRelease();
            }
        }
        finally
        {
            _requestActivated = false;
            _displayRequest = null;
        }
    }
}

To use it, just use the Dispose pattern:

using (var helper = new DisplayRequestHelper())
{
    helper.PreventDisplayToTurnOff();
}

This way, you’re sure that the active request are release as soon as possible!

 

Happy coding!

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

One Response

Add Comment Register



Leave a Reply