分享C#实现系统休眠或静止休眠的方法

您所在的位置:网站首页 拨号1是什么代码 分享C#实现系统休眠或静止休眠的方法

分享C#实现系统休眠或静止休眠的方法

2023-07-02 19:42| 来源: 网络整理| 查看: 265

class SystemSleepManagement

   {

       //定义API函数

       [DllImport("kernel32.dll")]

       static extern uint SetThreadExecutionState(ExecutionFlag flags);

       [Flags]

       enum ExecutionFlag : uint

       {

           System = 0x00000001,

           Display = 0x00000002,

           Continus = 0x80000000,

       }

       ///

       ///阻止系统休眠,直到线程结束恢复休眠

       ///

       /// 是否阻止关闭显示器

       public static void PreventSleep(bool includeDisplay = false)

       {

           if (includeDisplay)

               SetThreadExecutionState(ExecutionFlag.System | ExecutionFlag.Display | ExecutionFlag.Continus);

           else

               SetThreadExecutionState(ExecutionFlag.System | ExecutionFlag.Continus);

       }

       ///

       ///恢复系统休眠

       ///

       public static void ResotreSleep()

       {

           SetThreadExecutionState(ExecutionFlag.Continus);

       }

       ///

       ///重置系统休眠计时器

       ///

       /// 是否阻止关闭显示器

       public static void ResetSleepTimer(bool includeDisplay = false)

       {

           if (includeDisplay)

               SetThreadExecutionState(ExecutionFlag.System | ExecutionFlag.Display);

           else

               SetThreadExecutionState(ExecutionFlag.System);

       }

   }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3