SystemSleep.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace DayOf1440.Utils
  8. {
  9. public static class SystemSleep
  10. {
  11. [DllImport("kernel32")]
  12. private static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags);
  13. [Flags]
  14. private enum ExecutionState : uint
  15. {
  16. /// <summary>
  17. /// Forces the system to be in the working state by resetting the system idle timer.
  18. /// </summary>
  19. SystemRequired = 0x01,
  20. /// <summary>
  21. /// Forces the display to be on by resetting the display idle timer.
  22. /// </summary>
  23. DisplayRequired = 0x02,
  24. /// <summary>
  25. /// 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.
  26. /// </summary>
  27. [Obsolete("This value is not supported.")]
  28. UserPresent = 0x04,
  29. /// <summary>
  30. /// Enables away mode. This value must be specified with <see cref="Continuous"/>.
  31. /// <para />
  32. /// 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.
  33. /// </summary>
  34. AwaymodeRequired = 0x40,
  35. /// <summary>
  36. /// 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.
  37. /// </summary>
  38. Continuous = 0x80000000,
  39. }
  40. /// <summary>
  41. /// 设置此线程此时开始一直将处于运行状态,此时计算机不应该进入睡眠状态。
  42. /// 此线程退出后,设置将失效。
  43. /// 如果需要恢复,请调用 <see cref="RestoreForCurrentThread"/> 方法。
  44. /// </summary>
  45. /// <param name="keepDisplayOn">
  46. /// 表示是否应该同时保持屏幕不关闭。
  47. /// 对于游戏、视频和演示相关的任务需要保持屏幕不关闭;而对于后台服务、下载和监控等任务则不需要。
  48. /// </param>
  49. public static void PreventForCurrentThread(bool keepDisplayOn = true)
  50. {
  51. SetThreadExecutionState(keepDisplayOn
  52. ? ExecutionState.Continuous | ExecutionState.SystemRequired | ExecutionState.DisplayRequired
  53. : ExecutionState.Continuous | ExecutionState.SystemRequired);
  54. }
  55. /// <summary>
  56. /// 恢复此线程的运行状态,操作系统现在可以正常进入睡眠状态和关闭屏幕。
  57. /// </summary>
  58. public static void RestoreForCurrentThread()
  59. {
  60. SetThreadExecutionState(ExecutionState.Continuous);
  61. }
  62. /// <summary>
  63. /// 重置系统睡眠或者关闭屏幕的计时器,这样系统睡眠或者屏幕能够继续持续工作设定的超时时间。
  64. /// </summary>
  65. /// <param name="keepDisplayOn">
  66. /// 表示是否应该同时保持屏幕不关闭。
  67. /// 对于游戏、视频和演示相关的任务需要保持屏幕不关闭;而对于后台服务、下载和监控等任务则不需要。
  68. /// </param>
  69. public static void ResetIdle(bool keepDisplayOn = true)
  70. {
  71. SetThreadExecutionState(keepDisplayOn
  72. ? ExecutionState.SystemRequired | ExecutionState.DisplayRequired
  73. : ExecutionState.SystemRequired);
  74. }
  75. }
  76. }