Recently looked under the computer housekeeper inside to prevent the system to enter the standby mode of implementation, in fact, very simple, haha. Take a look at the description of this API on MSDN: http://msdn.microsoft.com/en-us/library/aa373208 (v=vs.85). aspx
The following is reproduced
-----------------------------------------------------------------------------------------------
Storm video will prevent the screen saver from running when it is playing, and will prevent the system from shutting down the monitor and entering the standby state. The following is the use of C # to implement this function.
1. Prevent screen saver from running
The rationale is to call the Windows API function SystemParametersInfo () to turn off the screen saver when playing, and then reopen it when the playback is finished. The code is as follows:
[C-sharp]View PlainCopy
- Defining API Functions
- [DllImport ("user32.dll")]
- Static extern bool SystemParametersInfo (UINT uiaction, bool Uiparam, ref bool Pvparam, uint F Winini);
- Const UINT spi_getscreensaveactive = 0x0010;
- Const UINT spi_setscreensaveactive = 0x0011;
- Const UINT spif_sendchange = 0x0002;
- Const UINT spif_sendwininichange = Spif_sendchange;
- Called, where false in the function is the setting that works, and the active variable is used when reading settings, and there is no practical meaning.
- BOOL active = false;
- SystemParametersInfo (Spi_setscreensaveactive, false, ref active, Spif_sendwininichange);
C # Calling the Windows API is still handy, but data type conversions can be cumbersome, but they're much more familiar.
Another case is that the screen saver did not open. This can be done by spi_getscreensaveactive to get the initial configuration, and finally restore.
Of course, there are other ways to do this, such as a form application that can override the Form.wndproc () method to intercept the system message Sc_screensave to terminate the screensaver, or use Windows Hook technology to intercept the message.
2. Prevent the system from shutting down the monitor or entering the standby state
The same is called the Windows API function, SetThreadExecutionState (). This function resets the idle time of the system (IdleTime), but does not prevent the screensaver from running. The code is as follows:
[C-sharp]View PlainCopy
- Defining API Functions
- [DllImport ("kernel32.dll")]
- Static extern uint setthreadexecutionstate (uint esflags);
- Const UINT es_system_required = 0x00000001;
- Const UINT es_display_required = 0x00000002;
- Const UINT es_continuous = 0x80000000;
- Called when playing
- SetThreadExecutionState (es_continuous | es_display_required | es_system_required);
- Call after playback is finished
- SetThreadExecutionState (es_continuous);
Or you can use a System.Timers.Timer to call setthreadexecutionstate at regular intervals (es_display_required | es_system_required), see a person likes it.
The specific use of the above functions can be referred to MSDN. WindowsAPI is really magical and deserves to be studied in the future.
http://blog.csdn.net/magictong/article/details/6560634
Block screensaver operation, display and system standby (two ways to use SystemParametersInfo and setthreadexecutionstate)