开发者

How do I turn off the windows screen saver if it is running using C#?

Ok, so I found some code to check if a screensaver is running and kill it if I want to. This doesn't seem to work on Windows 7 PCs. Does anyone know how this code can be modified, or provide new code to accomplish this?

My application is designed to run in th开发者_C百科e background until a particular event occurs, and then create and display a full screen notification. This needs to be displayed even if a screen saver is currently up.


The easiest way is to fake a mouse move event with x=y=0 by calling SendInput().


The following question may provide some insight:

How to turn screensaver on (windows 7) by a code (in cmd)?

However, what if the machine is locked? I don't think you'll be able to display any application on top of the lock screen unless it is actually a screensaver (and even then it may not be possible)


Assuming you don't have a password-protected screensaver: (from http://support.microsoft.com/kb/140723)

PostMessage (GetActiveWindow(), WM_CLOSE, 0, 0L);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜