28

For a particular application, I need the screen saver to be disabled while it's running. The operator COULD manually turn it off, and then back on later, but the easiest thing to do would be to just keep the screen saver at bay while the application is running.

How do I do this? I've found code for actually turning off the screen saver with SPI_SETSCREENSAVEACTIVE, but I don't think that's what I want.

Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90

5 Answers5

41

EDIT - I have an updated answer using the modern Power Availability Request API (supersedes SetThreadExecutionState) here: https://stackoverflow.com/a/63632916/67824

[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
    ES_SYSTEM_REQUIRED = 0x00000001,
    ES_DISPLAY_REQUIRED = 0x00000002,
    // Legacy flag, should not be used.
    // ES_USER_PRESENT   = 0x00000004,
    ES_AWAYMODE_REQUIRED = 0x00000040,
    ES_CONTINUOUS = 0x80000000,
}

public static class SleepUtil
{
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
}

public void PreventSleep()
{
    if (SleepUtil.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
        | EXECUTION_STATE.ES_DISPLAY_REQUIRED  
        | EXECUTION_STATE.ES_SYSTEM_REQUIRED 
        | EXECUTION_STATE.ES_AWAYMODE_REQUIRED) == 0) //Away mode for Windows >= Vista
        SleepUtil.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
            | EXECUTION_STATE.ES_DISPLAY_REQUIRED 
            | EXECUTION_STATE.ES_SYSTEM_REQUIRED); //Windows < Vista, forget away mode
}

Credit: P/Invoke, deadpoint

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
  • 3
    Tested this with w7 x64, worked like a charm! No screen saver or monitor energy saving functions were triggered. +1! – Gerardo Grignoli Jan 02 '11 at 17:37
  • 1
    Yep, I'm using it too :) – Ohad Schneider Jan 02 '11 at 18:36
  • 1
    Works great! Simplest working solution I could find. Thanks! – Nelson Reis May 17 '12 at 15:21
  • 3
    [`ES_AWAYMODE_REQUIRED` should not be used.](http://www.blackwasp.co.uk/DisableScreensaver.aspx) It works for me without this flag on Win10. – xmedeko Oct 05 '15 at 18:22
  • 3
    SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED); - work perfect on win10 to disable screensaver – user1005462 Oct 06 '15 at 12:33
  • Worth noting that when using ES_AWAYMODE_REQIURED the computer will not be able to enter into sleep mode, even if it's turned on manually by the user. – user1414213562 Nov 01 '16 at 13:30
  • I'm using this on a program targeting .NET 4.5, but the screen still dims, and turns black, and system sleeps after a while. What else can I try ?? – mrid Mar 11 '19 at 05:20
17

theoldnewthing has your answer: Use SetThreadExecutionState(ES_DISPLAY_REQUIRED).

This is used by video players and PowerPoint.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Serafeim
  • 14,962
  • 14
  • 91
  • 133
3
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
            | EXECUTION_STATE.ES_DISPLAY_REQUIRED 
            | EXECUTION_STATE.ES_SYSTEM_REQUIRED);

This is not helpful on XP.

In fact, this function is not cross operable between diferent Windows versions (although it works pretty well in Windows Vista or higher)... In Windows XP / 2003 this function shall be called with ES_USER_PRESENT | ES_CONTINUOUS (both should be called)... This will reset periodically both system and display idle timers... In other Windows versions, it's recommended that you use ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS | ES_AWAYMODE_REQUIRED...

Community
  • 1
  • 1
AVladislav
  • 31
  • 1
2

SystemParametersInfo with SPI_SETSCREENSAVEACTIVE is the normal way to do this. However, it doesn't disable screen locking.

Jasper Bekkers
  • 6,711
  • 32
  • 46
0

try making your form topmost value true it works for me screen-saver never came even after the idle time...

deepu
  • 1,993
  • 6
  • 42
  • 64