Stop Screensaver programmatically
Asked Answered
N

1

2

How can I stop the screensaver while it's running? without moving the mouse or pressing a key on the keyboard. My applications input is from a card reader, if the screen saver is running my application is still working fine but the screen saver doesn't stop when an input is received on the card reader.

I've tried this http://www.codeproject.com/Articles/17067/Controlling-The-Screen-Saver-With-C , doesn't seem to work for me.

I tried faking a mouse movement like on this thread How do I turn off the windows screen saver if it is running using C#? and as mentioned in the thread, it doesn't seem to work on windows 8(i'm running on windows 8.1).

I even tried SendKeys.

Most other questions/examples I see are for stopping the screen saver from starting which I don't want. I want the screen saver to start and stopped if I get an input in my card reader.

Nashville answered 9/7, 2016 at 14:20 Comment(9)
Did you write the screen saver?Tumble
@Tumble what do you mean? it's just a normal screen saver under personalisation menu on windows.Nashville
I assume you mean no then. In which case, you may have difficulty doing this. If you write the screen saver yourself then, it would be far simpler to do.Tumble
On your smart card reader input, you can change the registry value like this -Call Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop", "ScreenSaveActive", "1") This works on windows 7. Not sure about Windows 8.Advantageous
@Advantageous what is this suppose to do? I checked the value and it's currently 1 already.Nashville
you can use 0 and 1 to disable and enable Screen Saver.Advantageous
@Advantageous you said Disable and enable, but what if the screen saver is already running? as in you can see it in the screen, will this stop it?Nashville
Can you try the KillScreenSaver in this answer: https://mcmap.net/q/506099/-turn-display-on-and-kill-screensaverMckamey
Here is a duplicate with an answer https://mcmap.net/q/474472/-prevent-screen-from-sleeping-with-c/495455Leoraleos
C
2

Use SetThreadExecutionState this winAPI to tell the operating system that the thread is in use, even if the user is not interacting with the computer. These will prevent to appear screen saver and stop the machine from being suspended automatically.

Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.

There are series of flags to specify a new state for the current thread:

  • ES_AWAYMODE_REQUIRED (0x00000040) : Enables away mode.
  • ES_DISPLAY_REQUIRED (0x00000002) : Forces the display to be on by resetting the display idle timer.
  • ES_SYSTEM_REQUIRED (0x00000001) : Forces the system to be in the working state by resetting the system idle timer.
  • ES_CONTINUOUS (0x80000000) : Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.

As this is an winAPI, so you have to PInvoke this :

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

User-Defined Types:

[FlagsAttribute]
public enum EXECUTION_STATE :uint
{
   ES_AWAYMODE_REQUIRED = 0x00000040,
   ES_CONTINUOUS = 0x80000000,
   ES_DISPLAY_REQUIRED = 0x00000002,
   ES_SYSTEM_REQUIRED = 0x00000001
 }

Here below is the calling procedure:

//To stop screen saver and monitor power off event
//You can combine several flags and specify multiple behaviors with a single call
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);

//To reset or allow those event again you have to call this API with only ES_CONTINUOUS
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);//This will reset as normal

According to MSDN this API is safe to use.

The system maintains a count of applications that have called SetThreadExecutionState. The system tracks each thread that calls SetThreadExecutionState and adjusts the counter accordingly. If this counter reaches zero and there has not been any user input, the system enters sleep.

If the Application crashed before resetting flag, the System will adjust and will reset automatically.

Corson answered 2/5, 2020 at 16:23 Comment(1)
Very clear answer deserving more upvotes. I know the original question was about preventing screen-saver, but I'll attest the SetThreadExecutionState is absolutely effective for preventing the lock-screen when there is no mouse or keyboard activity.Lorsung

© 2022 - 2025 — McMap. All rights reserved.