我有一個問題,其中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