[英]PowerShell wait application to launch
I have the following script to launch an application: 我有以下脚本来启动应用程序:
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms
$args = "arguments"
$proc = Start-Process -PassThru "path" -ArgumentList $args
start-sleep -Seconds 5
[Microsoft.VisualBasic.Interaction]::AppActivate($proc.Id)
[System.Windows.Forms.SendKeys]::SendWait("~")
I use this script to launch several windows forms applications that need user interaction to start (ie push a start button). 我使用这个脚本启动几个需要用户交互启动的Windows窗体应用程序(即按下启动按钮)。 So far I've been sleeping the script 5 seconds to allow the application to launch.
到目前为止,我一直在睡眠脚本5秒钟,以允许应用程序启动。 I tried to run this script in a variety of computers with different processing capabilities, but did not work in all computers, some launch the application slower than others, when the app took more than 5 seconds to launch the app the script fails, because the AppActivate could not find the PID.
我尝试在具有不同处理能力的各种计算机上运行此脚本,但在所有计算机上都不起作用,有些启动应用程序的速度比其他计算机慢,当应用程序花了5秒多时间启动应用程序脚本失败时,因为AppActivate无法找到PID。 I don't want to try different sleeping times for each computer, because I have to run this script in more than 100 computers at boot time.
我不想为每台计算机尝试不同的休眠时间,因为我必须在启动时在100多台计算机上运行此脚本。
I would like to know if there is a way to wait for the application to launch in a event driven way. 我想知道是否有办法等待应用程序以事件驱动的方式启动。
UPDATE: 更新:
The WaitForInputIdle does not work in all applications I tried to start. WaitForInputIdle在我尝试启动的所有应用程序中都不起作用。 It returns immediately (successfully because it's returning true) and the AppActive method throws an exception.
它立即返回(成功,因为它返回true),AppActive方法抛出异常。
I suggest you to avoid using "Sleep" to synchronize system objects (it's never a good solution). 我建议你不要使用“Sleep”来同步系统对象(这绝不是一个好的解决方案)。 As far as you are starting Windows Forms application I suggest you to use Process.WaitForInputIdle Method.
至于您启动Windows窗体应用程序,我建议您使用Process.WaitForInputIdle方法。
$p = [diagnostics.process]::start("notepad.exe", "D:\temp\1mbfile.txt")
$p.WaitForInputIdle(5000);
using it in a loop you can test alternatively test if the input is idle (application is started and waiting) or if the application is stopped ( $p.HasExited -eq $true
). 在循环中使用它可以测试或者测试输入是否空闲(应用程序已启动并等待)或应用程序是否已停止(
$p.HasExited -eq $true
)。
do
{
if ($p.HasExited -eq $true)
{
break
}
} while ($p.WaitForInputIdle(5000) -ne $true)
So, I finally wrote this workaround since WaitForInputIdle does not work with all my applications and I was not able to figure out why. 所以,我终于编写了这个解决方法,因为WaitForInputIdle不适用于我的所有应用程序,我无法弄清楚原因。
do{
if ($proc.MainWindowHandle -ne 0)
{
break
}
$proc.Refresh()
} while ($true)
I am not sure if this is the best approach; 我不确定这是否是最佳方法; anyway, I wanted to share this.
无论如何,我想分享这个。
Neither of the other answers waited long enough for my script, so I came up with this, which waits until the process stops using the cpu. 这两个答案都没有等到我的脚本足够长的时间,所以我想出了这个,等待进程停止使用cpu。
do{
$ProcCpu = $Process.CPU
Start-Sleep -Milliseconds 100
} until($ProcCpu -eq $Process.CPU)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.