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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
crimson589
  • 1,238
  • 1
  • 20
  • 36
  • Did you write the screen saver? – ManoDestra Jul 09 '16 at 14:34
  • @ManoDestra what do you mean? it's just a normal screen saver under personalisation menu on windows. – crimson589 Jul 09 '16 at 14:35
  • 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. – ManoDestra Jul 09 '16 at 14:38
  • 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. – Sanket Jul 09 '16 at 15:20
  • @Sanket what is this suppose to do? I checked the value and it's currently 1 already. – crimson589 Jul 09 '16 at 15:27
  • you can use 0 and 1 to disable and enable Screen Saver. – Sanket Jul 09 '16 at 15:28
  • @Sanket 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? – crimson589 Jul 10 '16 at 08:26
  • Can you try the KillScreenSaver in this answer: http://stackoverflow.com/a/36292070/578411 – rene Jul 14 '16 at 19:23
  • Here is a duplicate with an answer https://stackoverflow.com/q/49045701/495455 – Jeremy Thompson Mar 01 '18 at 09:35

1 Answers1

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.

yeasir007
  • 2,110
  • 2
  • 28
  • 43
  • 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. – mdisibio Oct 30 '20 at 18:07