Пример #1
0
        public void SuppressStandby()
        {
            // Clear current power request if there is any.
            if (currentPowerRequest != IntPtr.Zero)
            {
                NativeAPIs.PowerClearRequest(currentPowerRequest, PowerRequestType.PowerRequestAwayModeRequired);
                currentPowerRequest = IntPtr.Zero;
            }

            // Create new power request.
            NativeAPIs.POWER_REQUEST_CONTEXT pContext;
            pContext.Flags   = NativeAPIs.POWER_REQUEST_CONTEXT_SIMPLE_STRING;
            pContext.Version = NativeAPIs.POWER_REQUEST_CONTEXT_VERSION;
            // This is the reason for standby suppression. It is shown when the command "powercfg -requests" is executed.
            pContext.SimpleReasonString = "Standby suppressed by ButtonHandler.exe";

            currentPowerRequest = NativeAPIs.PowerCreateRequest(ref pContext);

            if (currentPowerRequest == IntPtr.Zero)
            {
                // Failed to create power request.
                var error = Marshal.GetLastWin32Error();

                if (error != 0)
                {
                    throw new Win32Exception(error);
                }
            }

            bool success = NativeAPIs.PowerSetRequest(currentPowerRequest, PowerRequestType.PowerRequestAwayModeRequired);

            if (!success)
            {
                // Failed to set power request.
                currentPowerRequest = IntPtr.Zero;
                var error = Marshal.GetLastWin32Error();

                if (error != 0)
                {
                    throw new Win32Exception(error);
                }
            }
        }
Пример #2
0
        public void EnableStandby()
        {
            // Only try to clear power request if any power request is set.
            if (currentPowerRequest != IntPtr.Zero)
            {
                var success = NativeAPIs.PowerClearRequest(currentPowerRequest, PowerRequestType.PowerRequestAwayModeRequired);

                if (!success)
                {
                    // Failed to clear power request.
                    currentPowerRequest = IntPtr.Zero;
                    var error = Marshal.GetLastWin32Error();

                    if (error != 0)
                    {
                        throw new Win32Exception(error);
                    }
                }
                else
                {
                    currentPowerRequest = IntPtr.Zero;
                }
            }
        }