Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to disable the screen saver / screen locking in Windows C#/.Net [closed]

Tags:

c#

screensaver

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.

like image 847
Clinton Pierce Avatar asked Oct 27 '08 20:10

Clinton Pierce


People also ask

How do I turn off the lock screen screensaver?

Press the Windows key , type Change screen saver, and then press Enter . Under the Screen saver section, click the down arrow on the right side of the selection box (A). To enable, select a screen saver from the list. To disable, select None.

How do I stop my screen from locking when idle?

Click Start>Settings>System>Power and Sleep and on the right side panel, change the value to “Never” for Screen and Sleep. Hope the information is helpful.


1 Answers

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

like image 102
Ohad Schneider Avatar answered Sep 20 '22 00:09

Ohad Schneider