Block screensaver operation, display and system standby (two ways to use SystemParametersInfo and setthreadexecutionstate)

Source: Internet
Author: User

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
  1. Defining API Functions
  2. [DllImport ("user32.dll")]
  3. Static extern bool SystemParametersInfo (UINT uiaction, bool Uiparam, ref bool Pvparam, uint F     Winini);
  4. Const UINT spi_getscreensaveactive = 0x0010;
  5. Const UINT spi_setscreensaveactive = 0x0011;
  6. Const UINT spif_sendchange = 0x0002;
  7. Const UINT spif_sendwininichange = Spif_sendchange;
  8. 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.
  9. BOOL active = false;
  10. 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
  1. Defining API Functions
  2. [DllImport ("kernel32.dll")]
  3. Static extern uint setthreadexecutionstate (uint esflags);
  4. Const UINT es_system_required = 0x00000001;
  5. Const UINT es_display_required = 0x00000002;
  6. Const UINT es_continuous = 0x80000000;
  7. Called when playing
  8. SetThreadExecutionState (es_continuous | es_display_required | es_system_required);
  9. Call after playback is finished
  10. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.