c#阻止系统休眠

#####1。需求: 大数量数据上传时,有时数据还未上传完成,系统就进入休眠状态。
如何保证在运行上传程序期间,系统不进入休眠状态。

计算机休眠

  1. 计算机进入睡眠后,进程会关闭.。
  2. c#阻止系统休眠 ,转自https://blog.csdn.net/WPwalter/article/details/103268540
  3. c# 系统休眠调

.NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态

在 Windows 系统中,一段时间不操作键盘和鼠标,屏幕便会关闭,系统会进入睡眠状态。但有些程序(比如游戏、视频和演示文稿)在运行过程中应该阻止屏幕关闭,否则屏幕总是关闭,会导致体验会非常糟糕。

本文介绍如何编写 .NET/C# 代码临时阻止屏幕关闭以及系统进入睡眠状态。

我们需要使用到一个 Windows API:

/// <summary>
/// Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.
/// </summary>
[DllImport("kernel32")]
private static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags);
————————————————
版权声明:本文为CSDN博主「walter lv」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/WPwalter/article/details/103268540


————————————————
版权声明:本文为CSDN博主「walter lv」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/WPwalter/article/details/103268540

使用到的枚举用 C# 类型定义是:

[Flags]
private enum ExecutionState : uint
{
    /// <summary>
    /// Forces the system to be in the working state by resetting the system idle timer.
    /// </summary>
    SystemRequired = 0x01,

    /// <summary>
    /// Forces the display to be on by resetting the display idle timer.
    /// </summary>
    DisplayRequired = 0x02,

    /// <summary>
    /// This value is not supported. If <see cref="UserPresent"/> is combined with other esFlags values, the call will fail and none of the specified states will be set.
    /// </summary>
    [Obsolete("This value is not supported.")]
    UserPresent = 0x04,

    /// <summary>
    /// Enables away mode. This value must be specified with <see cref="Continuous"/>.
    /// <para />
    /// Away mode should be used only by media-recording and media-distribution applications that must perform critical background processing on desktop computers while the computer appears to be sleeping.
    /// </summary>
    AwaymodeRequired = 0x40,

    /// <summary>
    /// Informs the system that the state being set should remain in effect until the next call that uses <see cref="Continuous"/> and one of the other state flags is cleared.
    /// </summary>
    Continuous = 0x80000000,
}

 

以上所有的注释均照抄自微软的官方 API 文档:

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate

API 封装

如果你擅长阅读英文,那么以上的 API 函数、枚举和注释足够你完成你的任务了。

不过,我这里提供一些封装,以应对一些常用的场景。

using System;
using System.Runtime.InteropServices;

namespace Walterlv.Windows
{
    /// <summary>
    /// 包含控制屏幕关闭以及系统休眠相关的方法。
    /// </summary>
    public static class SystemSleep
    {
        /// <summary>
        /// 设置此线程此时开始一直将处于运行状态,此时计算机不应该进入睡眠状态。
        /// 此线程退出后,设置将失效。
        /// 如果需要恢复,请调用 <see cref="RestoreForCurrentThread"/> 方法。
        /// </summary>
        /// <param name="keepDisplayOn">
        /// 表示是否应该同时保持屏幕不关闭。
        /// 对于游戏、视频和演示相关的任务需要保持屏幕不关闭;而对于后台服务、下载和监控等任务则不需要。
        /// </param>
        public static void PreventForCurrentThread(bool keepDisplayOn = true)
        {
            SetThreadExecutionState(keepDisplayOn
                ? ExecutionState.Continuous | ExecutionState.SystemRequired | ExecutionState.DisplayRequired
                : ExecutionState.Continuous | ExecutionState.SystemRequired);
        }

        /// <summary>
        /// 恢复此线程的运行状态,操作系统现在可以正常进入睡眠状态和关闭屏幕。
        /// </summary>
        public static void RestoreForCurrentThread()
        {
            SetThreadExecutionState(ExecutionState.Continuous);
        }

        /// <summary>
        /// 重置系统睡眠或者关闭屏幕的计时器,这样系统睡眠或者屏幕能够继续持续工作设定的超时时间。
        /// </summary>
        /// <param name="keepDisplayOn">
        /// 表示是否应该同时保持屏幕不关闭。
        /// 对于游戏、视频和演示相关的任务需要保持屏幕不关闭;而对于后台服务、下载和监控等任务则不需要。
        /// </param>
        public static void ResetIdle(bool keepDisplayOn = true)
        {
            SetThreadExecutionState(keepDisplayOn
                ? ExecutionState.SystemRequired | ExecutionState.DisplayRequired
                : ExecutionState.SystemRequired);
        }
    }
}

 

如果你对这段封装中的 keepDisplayOn 参数,也就是 ExecutionState.DisplayRequired 枚举不了解,看看下图直接就懂了。一个指的是屏幕关闭,一个指的是系统进入睡眠。

电源和睡眠

此封装后,使用则相当简单:

// 阻止系统睡眠,阻止屏幕关闭。
SystemSleep.PreventForCurrentThread();

// 恢复此线程曾经阻止的系统休眠和屏幕关闭。
SystemSleep.RestoreForCurrentThread();

 

或者:

// 重置系统计时器,临时性阻止系统睡眠和屏幕关闭。
// 此效果类似于手动使用鼠标或键盘控制了一下电脑。
SystemSleep.ResetIdle();

 

在使用 PreventForCurrentThread 这个 API 的时候,你需要避免程序对空闲时机的控制不好,导致屏幕始终不关闭。

如果你发现无论你设置了多么短的睡眠时间和屏幕关闭时间,屏幕都不会关闭,那就是有某个程序阻止了屏幕关闭,你可以:

查看有哪些程序会一直保持屏幕处于打开状态
找到是谁持续唤醒了计算机屏幕
————————————————
版权声明:本文为CSDN博主「walter lv」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/WPwalter/article/details/103268540

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统休眠后,音频设备可能会被禁用,导致应用程序没有声音。为了解决这个问题,你可以在应用程序中添加一个事件处理程序来检测系统休眠和唤醒事件,并在应用程序恢复时重新初始化音频设备。下面是一个示例代码: ```csharp using System; using System.Windows.Forms; using Microsoft.Win32; public class Form1 : Form { // 声明一个Windows消息常量 private const int WM_POWERBROADCAST = 0x218; // 声明一个枚举类型表示系统电源事件 private enum PowerBroadcastEventType : int { PBT_APMQUERYSUSPEND = 0x0, PBT_APMQUERYSTANDBY = 0x1, PBT_APMQUERYSUSPENDFAILED = 0x2, PBT_APMQUERYSTANDBYFAILED = 0x3, PBT_APMSUSPEND = 0x4, PBT_APMSTANDBY = 0x5, PBT_APMRESUMECRITICAL = 0x6, PBT_APMRESUMESUSPEND = 0x7, PBT_APMRESUMESTANDBY = 0x8, PBT_APMBATTERYLOW = 0x9, PBT_APMPOWERSTATUSCHANGE = 0xA, PBT_APMOEMEVENT = 0xB, PBT_APMRESUMEAUTOMATIC = 0x12 } // 声明一个委托类型表示处理电源事件的方法 private delegate void PowerBroadcastEventHandler(object sender, PowerBroadcastEventArgs e); // 声明一个事件表示系统电源事件 private event PowerBroadcastEventHandler PowerBroadcastEvent; // 声明一个方法处理系统电源事件 protected virtual void OnPowerBroadcastEvent(PowerBroadcastEventArgs e) { PowerBroadcastEventHandler handler = PowerBroadcastEvent; if (handler != null) { handler(this, e); } } // 重写WndProc方法以处理Windows消息 protected override void WndProc(ref Message m) { if (m.Msg == WM_POWERBROADCAST) { PowerBroadcastEventArgs e = new PowerBroadcastEventArgs((PowerBroadcastEventType)m.WParam.ToInt32()); OnPowerBroadcastEvent(e); } base.WndProc(ref m); } // 在窗体加载时初始化电源事件处理程序 private void Form1_Load(object sender, EventArgs e) { PowerBroadcastEvent += new PowerBroadcastEventHandler(Form1_PowerBroadcastEvent); } // 处理电源事件的方法 private void Form1_PowerBroadcastEvent(object sender, PowerBroadcastEventArgs e) { if (e.EventType == PowerBroadcastEventType.PBT_APMRESUMESUSPEND || e.EventType == PowerBroadcastEventType.PBT_APMRESUMESTANDBY || e.EventType == PowerBroadcastEventType.PBT_APMRESUMEAUTOMATIC) { // 在应用程序恢复时重新初始化音频设备 // TODO: 重新初始化音频设备的代码 } } } // 定义一个类表示电源事件参数 public class PowerBroadcastEventArgs : EventArgs { private PowerBroadcastEventType _eventType; public PowerBroadcastEventArgs(PowerBroadcastEventType eventType) { _eventType = eventType; } public PowerBroadcastEventType EventType { get { return _eventType; } } } ``` 这个代码片段定义了一个`Form1`窗体类,并在其加载时初始化了一个处理系统电源事件的事件处理程序。当系统电源事件发生时,将调用`Form1_PowerBroadcastEvent`方法,在应用程序恢复时重新初始化音频设备。请注意,你需要根据你的实际情况来编写重新初始化音频设备的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值