
私は電話ミラーリング プログラム ( scrcpy
) を持っていて、10 分後にノートパソコンをスリープ状態にするように設定しています。主にライブ TV を見るために使用していますが、実行中にノートパソコンの電源が切れてしまいます。Web で調べて、powercfg -requestsoverride
そのプログラムで を使用しようとしましたが、うまくいかないようです。どうすればいいでしょうか?
編集:ちなみに私はWindows 10を使用しています。
答え1
ディスプレイオンを維持- プログラムの実行中にスリープ状態になったりディスプレイがオフになったりしないようにプログラムを実行します
これは、Windows 10 に組み込まれているコンパイラを使用します (Windows Vista 以降)。3 つの VB.NET コンパイラと 3 つの C# コンパイラがあり、各テキスト ファイルを同じフォルダーにコピーし、バッチ ファイルをダブルクリックするだけでプログラムが作成されます。
これはAPI呼び出しを使用しますスレッド実行状態の設定(https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate) これは、プログラムが Windows にスリープしないように指示する方法です。
プログラムの実行中にスリープ状態になったりディスプレイがオフになったりしないようにプログラムを実行します。
使用方法
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
1 回または無期限に実行できます ( ) 。 使用例:-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
答え3
プロセスラッソ特定のプロセスが実行中の場合、PCがスリープしないようにする機能があります。これは睡眠を妨げるルールはディスプレイのみ、またはPC全体に適用できます。内部的には、スレッド実行状態の設定「Governor」サービスによって呼び出される API。