我有一个问题,其中SendMessage()函数导致脚本挂起,因此从不退出,虽然它正在运行SendMessage(它的任务完成)。无论如何要解决这个问题,因为我有一段时间从主脚本中杀死它。SendMessage导致脚本挂起
Stop-job -name offmon
或
Remove-job -name offmon -force
不会杀它。由于没有完成移除作业的力量,报告它不能杀死它。
我需要每天多次调用它,并且每次执行时都会产生一个新的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
感谢
编辑:!错别字
我不知道这是不是你的问题,但SendMessage的签名应该是SendMessage(IntPtr,uint,IntPtr,IntPtr)。 – OldFart 2012-03-27 14:52:50
我不这么认为,因为实际的命令经过,显示器关闭,没有任何错误,但我可能会误解。 – yaodin 2012-03-27 16:19:28
你是64位的吗?如果这样读全部在这里:http://stackoverflow.com/questions/9891585/sendmessage-is-causing-script-to-hang – 2012-03-28 05:20:15