Exemplo n.º 1
0
        /// <summary>
        /// Enable standby using ThreadExecutionState.
        /// </summary>
        private void EnableStandbyThreadExecutionState()
        {
            try
            {
                lock (lockObj)
                {
                    bool standbyWasSuspended = this.standbySuspended;
                    var  success             = SetThreadExecutionState(ES_CONTINUOUS);

                    if (success == 0)
                    {
                        var ex    = Win32ExceptionManager.GetWin32Exception();
                        var error = Marshal.GetLastWin32Error();
                        throw new WsapmException(Resources.Wsapm_Service.StandbyManager_StandbyEnableFailed, ex);
                    }

                    // Standby sucessfully enabled.
                    this.standbySuspended = false;

                    if (standbyWasSuspended)
                    {
                        this.log.WriteLine(Resources.Wsapm_Service.StandbyManager_StandbyEnabled, LogMode.Normal); // Only write log if standby was suspended before.
                    }
                }
            }
            catch (Exception ex)
            {
                this.log.WriteError(ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Suspend standby by SetThreadExecutionSTate because of a given CheckSuspendResult.
        /// </summary>
        /// <param name="result">The result why the standby should be suspended.</param>
        private void SuspendStandbyThreadExecutionState(CheckSuspendResult result)
        {
            try
            {
                lock (lockObj)
                {
                    var success = SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);

                    if (success == 0)
                    {
                        this.standbySuspended = false;
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Service.StandbyManager_SuspendStandbyFailed, ex);
                    }
                    else
                    {
                        this.standbySuspended = true;

                        if (!String.IsNullOrEmpty(result.Reason))
                        {
                            this.log.WriteLine(String.Format(Resources.Wsapm_Service.StandbyManager_StandbySuspendedWithReason, result.Reason), LogMode.Normal);
                        }
                        else
                        {
                            this.log.WriteLine(Resources.Wsapm_Service.StandbyManager_StandbySuspendedWithoutReason, LogMode.Verbose);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.log.WriteError(ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Suspend standby by PowerAvailabilityRequest becasue of a given CheckSuspendResult.
        /// </summary>
        /// <param name="result">The result why the standby should be suspended.</param>
        private void SuspendStandbyPowerAvailabilityRequest(CheckSuspendResult result)
        {
            try
            {
                lock (lockObj)
                {
                    // Clear current power request if there is any.
                    if (this.currentPowerRequest != IntPtr.Zero)
                    {
                        PowerClearRequest(this.currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);
                        this.currentPowerRequest = IntPtr.Zero;
                        this.standbySuspended    = false;
                    }

                    POWER_REQUEST_CONTEXT pContext;
                    pContext.Flags              = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
                    pContext.Version            = POWER_REQUEST_CONTEXT_VERSION;
                    pContext.SimpleReasonString = Resources.Wsapm_Service.StandbyManager_StandbySuspended;

                    this.currentPowerRequest = PowerCreateRequest(ref pContext);

                    if (this.currentPowerRequest == IntPtr.Zero)
                    {
                        this.standbySuspended = false;
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Service.StandbyManager_CreatePowerRequestFail, ex);
                    }

                    bool success = PowerSetRequest(this.currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);

                    if (!success)
                    {
                        this.standbySuspended    = false;
                        this.currentPowerRequest = IntPtr.Zero;
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Service.StandbyManager_SuspendStandbyFailed, ex);
                    }
                    else
                    {
                        this.standbySuspended = true;

                        if (!String.IsNullOrEmpty(result.Reason))
                        {
                            this.log.WriteLine(String.Format(Resources.Wsapm_Service.StandbyManager_StandbySuspendedWithReason, result.Reason), LogMode.Normal);
                        }
                        else
                        {
                            this.log.WriteLine(Resources.Wsapm_Service.StandbyManager_StandbySuspendedWithoutReason, LogMode.Verbose);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.log.WriteError(ex);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Enable standby using power availability request.
        /// </summary>
        private void EnableStandbyPowerAvailabilityRequest()
        {
            try
            {
                lock (lockObj)
                {
                    bool standbyWasSuspended = this.standbySuspended;

                    if (this.currentPowerRequest != IntPtr.Zero)
                    {
                        var success = PowerClearRequest(this.currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);

                        if (!success)
                        {
                            this.currentPowerRequest = IntPtr.Zero;
                            var ex = Win32ExceptionManager.GetWin32Exception();
                            throw new WsapmException(Resources.Wsapm_Service.StandbyManager_StandbyEnableFailed, ex);
                        }
                        else
                        {
                            this.currentPowerRequest = IntPtr.Zero;
                            this.standbySuspended    = false;
                        }
                    }

                    if (standbyWasSuspended)
                    {
                        this.log.WriteLine(Resources.Wsapm_Service.StandbyManager_StandbyEnabled, LogMode.Normal); // Only write log if standby was suspended before.
                    }
                }
            }
            catch (Exception ex)
            {
                this.log.WriteError(ex);
            }
        }