我有一个手机镜像程序 ( scrcpy
),并将笔记本电脑设置为 10 分钟后进入睡眠状态。我主要用它观看直播电视,但笔记本电脑在运行时会关闭。在网上阅读了相关信息后,我尝试使用powercfg -requestsoverride
该程序,但似乎不起作用。我该怎么办?
编辑:顺便说一下,我正在使用 Windows 10。
答案1
保持显示开启- 运行一个程序,防止程序运行时睡眠或关闭显示器
这使用了 Windows 10 中的内置编译器(自 Windows Vista 以来一直如此) - 有三个 VB.NET 编译器和三个 C# 编译器 - 只需将每个文本文件复制到同一个文件夹中,然后双击批处理文件即可制作程序。
这使用 API 调用设置线程执行状态(https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate) 这就是程序告诉窗口不要进入睡眠状态的方式。
运行一个程序,防止程序运行时睡眠或关闭显示器
使用
KeepDisplayOn "C:\windows\notepad"
KeepSystemOn "C:\windows\notepad"
@Echo Off
ECHO Three files follow
ECHO PreventSleep.bat
ECHO.
ECHO This file compiles KeepDisplayOn.vb and KeepSystemOn.vb to KeepDisplayOn.exe and KeepSystemOn.exe using the system VB.NET compiler.
ECHO.
ECHO Runs a program preventing sleeping or the display turning off while the program runs
ECHO.
ECHO To Use
ECHO KeepDisplayOn "C:\windows\notepad"
ECHO KeepSystemOn "C:\windows\notepad"
ECHO.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\KeepDisplayOn.vb" /out:"%~dp0\KeepDisplayOn.exe" /target:winexe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\KeepSystemOn.vb" /out:"%~dp0\KeepSystemOn.exe" /target:winexe
pause
'KeepSystemOn.vb
imports System.Runtime.InteropServices
Public Module MyApplication
Public Declare UNICODE Function SetThreadExecutionState Lib "Kernel32" (ByVal esFlags as Integer) as Integer
Public Const ES_AWAYMODE_REQUIRED = &h40
Public Const ES_CONTINUOUS = &h80000000
Public Const ES_DISPLAY_REQUIRED = &h2
Public Const ES_SYSTEM_REQUIRED = &h1
Public Const ES_USER_PRESENT = &h4
Public Sub Main ()
Dim wshshell as Object
Dim Ret as Integer
WshShell = CreateObject("WScript.Shell")
Ret = SetThreadExecutionState(ES_Continuous + ES_System_Required + ES_Awaymode_Required)
WshShell.Run(Command(), , True)
End Sub
End Module
'KeepDisplayOn.vb
imports System.Runtime.InteropServices
Public Module MyApplication
Public Declare UNICODE Function SetThreadExecutionState Lib "Kernel32" (ByVal esFlags as Integer) as Integer
Public Const ES_AWAYMODE_REQUIRED = &h40
Public Const ES_CONTINUOUS = &h80000000
Public Const ES_DISPLAY_REQUIRED = &h2
Public Const ES_SYSTEM_REQUIRED = &h1
Public Const ES_USER_PRESENT = &h4
Public Sub Main ()
Dim wshshell as Object
Dim Ret as Integer
WshShell = CreateObject("WScript.Shell")
Ret = SetThreadExecutionState(ES_Continuous + ES_Display_Required + ES_Awaymode_Required)
WshShell.Run(Command(), , True)
End Sub
End Module
从https://winsourcecode.blogspot.com/2020/05/keepdisplayon-runs-program-preventing.html
答案2
不需要编译 vbs 脚本,这可以通过 Powershell 脚本完成,我刚刚在GitHub(及以下)。
此脚本允许您传递要监视的进程列表,以防止系统(-requiresSystem
)或系统和显示器(-requiresDisplay
)休眠。如果相应列表中的进程处于焦点状态或通常未最小化/隐藏(取决于参数),则可以防止显示器休眠。-focusOnly
它可以运行一次或无限期地(-oneTime
),具有可自定义的轮询率(-pollingRate
)。
使用示例:
SnoozeGuard.ps1 -requiresDisplay "mstsc" -requiresSystem "handbrake" -focusOnly 0 -pollingRate 120 -oneTime 0
代码本身
param (
[string[]]$requiresSystem,
[string[]]$requiresDisplay,
[int]$pollingRate = 120,
[bool]$focusOnly = $true,
[bool]$oneTime = $false
)
# Check if PowerState type is already defined (GPT suggests this, not sure why, since for me it does need to do this: possible for compatibility reasons?)
if (-not ([System.Management.Automation.PSTypeName]'PowerState').Type) {
# Define the PowerState enumeration
Add-Type @"
using System;
[Flags]
public enum PowerState : uint
{
ES_CONTINUOUS = 0x80000000,
ES_SYSTEM_REQUIRED = 0x00000001,
ES_DISPLAY_REQUIRED = 0x00000002
}
"@
}
# Check if NativeMethods type is already defined
if (-not ([System.Management.Automation.PSTypeName]'NativeMethods').Type) {
# Define the NativeMethods class
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class NativeMethods
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint SetThreadExecutionState(uint esFlags);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
}
"@
}
if (-not ([System.Management.Automation.PSTypeName]'WindowMethods').Type) {
# DEfine WindowMethods class
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class WindowMethods {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd);
}
"@
}
function SetExecutionState {
param (
[PowerState]$esFlags
)
$state = [PowerState]::ES_CONTINUOUS -bor $esFlags
# Suppress the output of SetThreadExecutionState
$result = [NativeMethods]::SetThreadExecutionState($state)
if ($result -eq 0) {
Write-Host "Error: Unable to set execution state." -ForegroundColor Red
}
Get-StateNames -Value ([NativeMethods]::SetThreadExecutionState(0)) # Get the current state
}
function Get-StateNames {
param (
[uint32]$Value
)
if ($Value -band [PowerState]::ES_SYSTEM_REQUIRED -and $Value -band [PowerState]::ES_DISPLAY_REQUIRED) {
Write-Host "Fully awake" -ForegroundColor DarkGreen
}
elseif ($Value -band [PowerState]::ES_SYSTEM_REQUIRED) {
Write-Host "System is awake" -ForegroundColor DarkYellow
}
elseif ($Value -band [PowerState]::ES_DISPLAY_REQUIRED) {
Write-Host "Display is awake" -ForegroundColor DarkBlue
}
else {
Write-Host "Feeling sleepy" -ForegroundColor Gray
}
}
function IsProcessRunning {
param (
[string[]]$processName
)
foreach ($name in $processName) {
Write-Host "Searching for ""$name""..." -ForegroundColor Gray
$process = Get-Process -Name $name -ErrorAction SilentlyContinue
if ($process) {
Write-Host "Process ""$name"" found" -ForegroundColor Gray
return $true
}
}
return $false
}
function IsProcessFocused {
param (
[string]$name
)
$foregroundWindow = [NativeMethods]::GetForegroundWindow()
if ($foregroundWindow -eq [IntPtr]::Zero) {
return $false
}
$processId = 0
[NativeMethods]::GetWindowThreadProcessId($foregroundWindow, [ref]$processId) | Out-Null
$focusedProcess = Get-Process -Id $processId -ErrorAction SilentlyContinue
if ($focusedProcess -and $focusedProcess.ProcessName -eq $name) {
return $true
}
return $false
}
function IsProcessVisible {
param (
[string]$processName
)
# Get the process by name
$process = Get-Process -Name $processName -ErrorAction SilentlyContinue
# Check if the process is found
if ($process -ne $null) {
# Check if the main window is minimized or hidden
if ($process.MainWindowHandle -ne [IntPtr]::Zero) {
$mainWindow = $process.MainWindowHandle
$isMinimized = [WindowMethods]::IsIconic($mainWindow)
$isVisible = [WindowMethods]::IsWindowVisible($mainWindow)
if ($isMinimized -or -not $isVisible) {
return $false
} else {
return $true
}
}
}
return $false
}
while (($requiresSystem -ne $null -and $requiresSystem.Length -gt 0) -or ($requiresDisplay -ne $null -and $requiresDisplay.Length -gt 0)) {
if (IsProcessRunning $requiresDisplay) {
$executionState = [PowerState]::ES_SYSTEM_REQUIRED
foreach ($name in $requiresDisplay) {
if (($focusOnly -and (IsProcessFocused $name)) -or (-not $focusOnly -and ((IsProcessFocused $name) -or (IsProcessVisible $name)))) {
Write-Host "Process ""$name"" wants display" -ForegroundColor Gray
$executionState = $executionState -bor [PowerState]::ES_DISPLAY_REQUIRED
break # Exit the loop once one focused process is found
}
}
SetExecutionState -esFlags $executionState
} elseif (IsProcessRunning $requiresSystem) {
SetExecutionState -esFlags ES_SYSTEM_REQUIRED
} else {
Write-Host "No processes found" -ForegroundColor Gray
}
if ($oneTime) {
break
} else {
Start-Sleep -Seconds $pollingRate
}
}
# Release the execution state when the process is not running
SetExecutionState -esFlags ES_CONTINUOUS | Out-Null