windows 防止Windows在我的程序运行时进入睡眠状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/629240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:08:17  来源:igfitidea点击:

Prevent windows from going into sleep when my program is running?

windowswinapisleep

提问by

I have to stop windows from going into sleep when my program is running.

当我的程序运行时,我必须阻止 Windows 进入睡眠状态。

And I don't only want to prevent the sleep-timer, I also want to cancel the sleep-event if I press the sleep-button or in any other way actively tell the computer to sleep. Therefore SetThreadExecutionState is not enough.

而且我不仅要阻止睡眠定时器,而且如果我按下睡眠按钮或以任何其他方式主动告诉计算机进入睡眠状态,我还想取消睡眠事件。因此 SetThreadExecutionState 是不够的。

Or...I don't actually have to prevent the sleep completely, only delay it 5-10sec to allow my program to finish a task.

或者...我实际上不必完全阻止睡眠,只需延迟 5-10 秒即可让我的程序完成任务。

(I know that this is bad program behavior but it's only for personal use.)

(我知道这是不好的程序行为,但仅供个人使用。)

采纳答案by MarkusEgle

I had a problem like this with a hardware device connected via usb. XP /Vista would sleep/hibernate right in the middle of ... Great you say, when it resumes it can just continue. If the hardware is still connected!!! Users have the habit of pulling cables out whenever they feel like it.

我在通过 USB 连接的硬件设备上遇到了这样的问题。XP /Vista 会在...中途睡眠/休眠,你说的很好,当它恢复时它可以继续。如果硬件仍然连接!用户习惯于随时拔出电缆。

You need to handle XP and Vista

你需要处理 XP 和 Vista

Under XP trap the WM_POWERBROADCAST and look for the PBT_APMQUERYSUSPEND wparam.

在 XP 下捕获 WM_POWERBROADCAST 并查找 PBT_APMQUERYSUSPEND wparam。

   // See if bit 1 is set, this means that you can send a deny while we are busy
   if (message.LParam & 0x1)
   {
      // send the deny message
      return BROADCAST_QUERY_DENY;
   } // if
   else
   {
      return TRUE;
   } // else

Under Vista use SetThreadExecutionState like this

在 Vista 下像这样使用 SetThreadExecutionState

// try this for vista, it will fail on XP
if (SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED) == NULL)
{
   // try XP variant as well just to make sure 
   SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
}  // if 

and when you app has finished set it back to normal

当您的应用程序完成后将其恢复正常

// set state back to normal
SetThreadExecutionState(ES_CONTINUOUS);

回答by MarkusEgle

After considering vim's answer

在考虑了vim 的回答之后

"Using PowerCreateRequest, PowerSetRequest, and PowerClearRequest functions is the preferred method."

“使用 PowerCreateRequest、PowerSetRequest 和 PowerClearRequest 函数是首选方法。”

with the linked AvailabilityRequests.docx on msdnwhich is exhausting to get into it (too much to read), I have searched the web for a concrete example in c#that is based on the PowerCreateRequest and found http://go4answers.webhost4life.com/Example/problem-monitor-wakeup-service-windows7-12092.aspx[EDIT 2016 - isn't available anymore]

使用msdn上链接的 AvailabilityRequests.docx令人筋疲力尽(阅读太多),我在网上搜索了一个基于 PowerCreateRequest 的c#中的具体示例,并找到了http://go4answers.webhost4life.com /Example/problem-monitor-wakeup-service-windows7-12092.aspx[EDIT 2016 - 不再可用]

Copied and adapted it to my needs (PInvoke of CloseHandle copied from msdn):

复制并适应我的需要(从msdn复制的 CloseHandle 的 PInvoke ):

using System.Runtime.InteropServices;

    #region prevent screensaver, display dimming and automatically sleeping
    POWER_REQUEST_CONTEXT _PowerRequestContext;
    IntPtr _PowerRequest; //HANDLE

    // Availability Request Functions
    [DllImport("kernel32.dll")]
    static extern IntPtr PowerCreateRequest(ref POWER_REQUEST_CONTEXT Context);

    [DllImport("kernel32.dll")]
    static extern bool PowerSetRequest(IntPtr PowerRequestHandle, PowerRequestType RequestType);

    [DllImport("kernel32.dll")]
    static extern bool PowerClearRequest(IntPtr PowerRequestHandle, PowerRequestType RequestType);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    internal static extern int CloseHandle(IntPtr hObject);

    // Availablity Request Enumerations and Constants
    enum PowerRequestType
    {
        PowerRequestDisplayRequired = 0,
        PowerRequestSystemRequired,
        PowerRequestAwayModeRequired,
        PowerRequestMaximum
    }

    const int POWER_REQUEST_CONTEXT_VERSION = 0;
    const int POWER_REQUEST_CONTEXT_SIMPLE_STRING = 0x1;
    const int POWER_REQUEST_CONTEXT_DETAILED_STRING = 0x2;

    // Availablity Request Structures
    // Note:  Windows defines the POWER_REQUEST_CONTEXT structure with an
    // internal union of SimpleReasonString and Detailed information.
    // To avoid runtime interop issues, this version of 
    // POWER_REQUEST_CONTEXT only supports SimpleReasonString.  
    // To use the detailed information,
    // define the PowerCreateRequest function with the first 
    // parameter of type POWER_REQUEST_CONTEXT_DETAILED.
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct POWER_REQUEST_CONTEXT
    {
        public UInt32 Version;
        public UInt32 Flags;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string
            SimpleReasonString;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct PowerRequestContextDetailedInformation
    {
        public IntPtr LocalizedReasonModule;
        public UInt32 LocalizedReasonId;
        public UInt32 ReasonStringCount;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string[] ReasonStrings;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct POWER_REQUEST_CONTEXT_DETAILED
    {
        public UInt32 Version;
        public UInt32 Flags;
        public PowerRequestContextDetailedInformation DetailedInformation;
    }
    #endregion



    /// <summary>
    /// Prevent screensaver, display dimming and power saving. This function wraps PInvokes on Win32 API. 
    /// </summary>
    /// <param name="enableConstantDisplayAndPower">True to get a constant display and power - False to clear the settings</param>
    private void EnableConstantDisplayAndPower(bool enableConstantDisplayAndPower)
    {
        if (enableConstantDisplayAndPower)
        {
            // Set up the diagnostic string
            _PowerRequestContext.Version = POWER_REQUEST_CONTEXT_VERSION;
            _PowerRequestContext.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
            _PowerRequestContext.SimpleReasonString = "Continuous measurement"; // your reason for changing the power settings;

            // Create the request, get a handle
            _PowerRequest = PowerCreateRequest(ref _PowerRequestContext);

            // Set the request
            PowerSetRequest(_PowerRequest, PowerRequestType.PowerRequestSystemRequired);
            PowerSetRequest(_PowerRequest, PowerRequestType.PowerRequestDisplayRequired);
        }
        else
        {
            // Clear the request
            PowerClearRequest(_PowerRequest, PowerRequestType.PowerRequestSystemRequired);
            PowerClearRequest(_PowerRequest, PowerRequestType.PowerRequestDisplayRequired);

            CloseHandle(_PowerRequest);
        }
    }

回答by vim

Using PowerCreateRequest, PowerSetRequest, and PowerClearRequest functions is the preferred method. Details and sample code (C/C#) are inside http://msdn.microsoft.com/en-us/library/windows/hardware/gg463205.aspx

使用 PowerCreateRequest、PowerSetRequest 和 PowerClearRequest 函数是首选方法。详细信息和示例代码(C/C#)位于http://msdn.microsoft.com/en-us/library/windows/hardware/gg463205.aspx

回答by Zooba

The same technique applies as for preventing the screensaver should be used. See Programmatically prevent Windows screensaver from starting.

应使用与防止屏幕保护程序相同的技术。请参阅以编程方式阻止 Windows 屏幕保护程序启动

Note that some security settings can override this (forcing computers to lock after a certain time is one).

请注意,某些安全设置可以覆盖此设置(强制计算机在特定时间后锁定是一种方法)。

回答by Greg Dean

How about waking it back up if it goes to sleep?

如果它进入睡眠状态,如何唤醒它?

http://www.enterprisenetworksandservers.com/monthly/art.php?1049

http://www.enterprisenetworksandservers.com/monthly/art.php?1049