Search code examples
c#winapivideo

WPF animation should not be interrupted by screensaver


In Windows, as long as a video is playing via the media player, the screensaver won't start. I have a technical animation in WPF running on a digital signage system, and I am searching for ways, that Windows behaves similar as while playing a video in the media player (no screensaver activation etc.).

Brute force solutions work, like sending a keyboard event or a mouse move event as in Interrupt an active screensaver programmatically?. However those are pretty hard workarounds.

I would assume there is a special message (like WM_ACTIVATE) to be sent to the Windows message queue via the Windows32 API, while a video or an animation is playing to notice the OS about it. Google and checking winuser.h did not yield results. Has anybody an idea? Thank's a lot!


Solution

  • Thank you for the comments on how to solve the question with SetThreadExecutionState. My solution now looks the following (inside a test class) and I've tested it on the current Windows 10 Enterprise and Windows 11 Pro.

    private enum ExecutionState : uint
    {
        EsContinuous = 0x80000000,
        EsDisplayRequired = 0x00000002,
        EsSystemRequired = 0x00000001
    }
    
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern uint SetThreadExecutionState(ExecutionState esFlags);
    
    public void Test()
    {
        SetThreadExecutionState(
            ExecutionState.EsContinuous
            | ExecutionState.EsDisplayRequired
            | ExecutionState.EsSystemRequired);
    }