Thursday, May 17, 2012

Using SetThreadExecutionState to programmatically disable the screensaver

BlackWasp - Disabling the Screensaver Programmatically

"...

Why Disable the Screensaver?

Screensaver software was originally created in order that still images were not shown on a computer display when the system was inactive. Instead, the screensaver would show moving images or a dark screen. Brightly coloured, static images can cause permanent screen damage, known as screen burn, in displays based upon cathode ray tube (CRT) or plasma technology. They can also cause temporary image persistence in liquid crystal displays (LCD). Although image persistence is temporary, it can be distracting so the use of a screensaver is often recommended for such displays.

If a screensaver has been enabled by the user, Microsoft Windows will show it after a period of inactivity. Windows has two built-in timers, known as the display idle timer and the system idle timer. These are used to determine the amount of time that has passed since the user provided some input, such as pressing a key, moving the mouse or touching a touch-sensitive screen. They are reset to zero in response to any of these actions. When the display idle timer reaches a pre-configured value, the screensaver starts. If the system idle timer expires, the system may be suspended. The lengths of time involved and whether the computer is put to sleep or not are configured in the screensaver and power options of the Control Panel.

Some software applications require that the screensaver is not displayed or that the machine is prevented from sleeping. For example, if you develop a video player or some presentation software, your users would not want the presentation to be interrupted by the screensaver. If your software provides a continuous service, such as acting as a fax server, you may want the screensaver enabled but you will want to prevent the computer from suspending automatically.

SetThreadExecutionState

The .NET framework classes don't provide a way to disable the screensaver or sleep mode, so we need to use Platform Invocation Services (P/Invoke) to call a Windows API function named, "SetThreadExecutionState". This function tells the operating system that the thread is in use, even if the user is not interacting with the computer. This can prevent the display from being hidden and stop the machine from being suspended automatically.

...

imageimage..."

This is one of those things I KNOW I will need in six to 12 months, but will, for the life of me, not be able to find again. So I caching it here for future reference (and for your ed-u-ma-kation too ;)

No comments: