gpt4 book ai didi

powershell - SendMessage 导致脚本挂起

转载 作者:行者123 更新时间:2023-12-03 22:52:18 27 4
gpt4 key购买 nike

我遇到了一个问题,即 SendMessage() 函数导致脚本挂起并因此永远不会退出,尽管它正在像它应该的那样运行 SendMessage(它的任务完成)。有没有办法解决这个问题,因为我有一段时间从主脚本中杀死它。

Stop-job -name offmon

或者
Remove-job -name offmon -force

不会杀死它。如果没有对 remove-job 施加的力,它会报告它无法杀死它,因为它尚未完成。

我每天需要多次调用它,每次调用它都会产生一个新的 powershell.exe,占用大约 30M 的内存。

注意:如果你运行它,代码将关闭你的显示器,并且“@”需要在行的开头(不能用标签页来看起来不错)。
start-job -Name offmon -ScriptBlock { 
$HWND = -1
$WM_SYSCOMMAND = 0x0112
$SC_MONITORPOWER = 0xF170
$MONITOR_ON = -1
$MONITOR_OFF = 2
#Store the C# signature of the SendMessage function.
$signature = @"
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
"@
#Add the SendMessage function as a static method of a class
$SendMessage = Add-Type -MemberDefinition $signature -Name "Win32SendMessage" -Namespace Win32Functions -PassThru
#Invoke the SendMessage Function
$SendMessage::SendMessage($HWND, $WM_SYSCOMMAND, $SC_MONITORPOWER, $MONITOR_OFF)
exit}

此外,这在没有开始工作的情况下也会挂起,所以我认为它与 start-job causes scripts to hang. 无关。 MS Support .此外,这是 Win7Ent/2008R2。

谢谢!

编辑:错别字

最佳答案

更改 SendMessagePostMessage .它对我有用。

不同的是PostMessage异步 它不必等待任何响应。

关于powershell - SendMessage 导致脚本挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9891585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com