c# - 使用 C# 防止屏幕休眠

标签 c# .net windows user-input

我创建了一个小型 C# 控制台应用程序来在屏幕上移动指针,希望这可以防止屏幕在几分钟后休眠/锁定。不幸的是,几分钟后屏幕仍然进入休眠状态。

有谁知道是否真的可以用 C# 编写一些类似于用户输入(鼠标或键盘)的东西,并防止屏幕自动休眠/锁定?

这是我所拥有的,我认为它可能会起作用。

class Program
{
    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);

    static Random rnd = new Random();

    static void Main(string[] args)
    {

        Rectangle screenRes = Screen.PrimaryScreen.Bounds;
        int widtMax = screenRes.Width;
        int heighMax = screenRes.Height;

        int w;
        int h;

        do
        {
            while (!Console.KeyAvailable)
            {
                w = rnd.Next(1, widtMax);
                h = rnd.Next(1, heighMax);

                SetCursorPos(w, h);
                System.Threading.Thread.Sleep(1000);
            }
        } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
    }
}

最佳答案

您可以使用 SetThreadExecutionState

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.

备注

Calling SetThreadExecutionState without ES_CONTINUOUS simply resets the idle timer; to keep the display or system in the working state, the thread must call SetThreadExecutionState periodically.

To run properly on a power-managed computer, applications such as fax servers, answering machines, backup agents, and network management applications must use both ES_SYSTEM_REQUIRED and ES_CONTINUOUS when they process events. Multimedia applications, such as video players and presentation applications, must use ES_DISPLAY_REQUIRED when they display video for long periods of time without user input. Applications such as word processors, spreadsheets, browsers, and games do not need to call SetThreadExecutionState.

DllImport

[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

枚举

[FlagsAttribute]
public enum EXECUTION_STATE :uint
{
     ES_AWAYMODE_REQUIRED = 0x00000040,
     ES_CONTINUOUS = 0x80000000,
     ES_DISPLAY_REQUIRED = 0x00000002,
     ES_SYSTEM_REQUIRED = 0x00000001
     // Legacy flag, should not be used.
     // ES_USER_PRESENT = 0x00000004
}

用法

void PreventSleep ()
{
    // Prevent Idle-to-Sleep (monitor not affected) (see note above)
    SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_AWAYMODE_REQUIRED);
}

2021 年 2 月 8 日更新:

如果有人正在寻找一个完整的例子,这是我在 github 上找到的一个实现了这个的项目:https://github.com/pedrolcl/screensaver-disabler

关于c# - 使用 C# 防止屏幕休眠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49045701/

相关文章:

c# - Win10 构建 : Loading assembly failed

windows - 系统运行时间超过 49 天时的 GetTickCount

windows - 计划任务.bat - 检查网络驱动器是否准备就绪

c# - 如何解析 Json.NET 多态对象?

c# - Dynamics CRM Online 对象缓存未正确缓存

c# - 使用 TCPDF 在 ASP.NET MVC 中生成 PDF

c# - 添加项目作为引用导致无法加载文件或程序集或其依赖项之一

c# - Process.Start 指定文化

c# - 用于将 PDF 转换为 HTML 的开源库/工具?

windows - 如何在 Eclipse 中打开文件并跳转到特定行?