mig*_*llo 3 powershell wait start-process
我正在使用预定的 PowerShell 脚本在循环中多次使用各种参数启动相同的进程。过程计算量很大,运行时间很长,我有数千次迭代。我注意到,因为它是一个预定脚本,所以它以低于正常的优先级运行。即使我碰到它,任何产生的进程仍然低于正常。因此,由于我在循环中运行此过程数千次,因此我需要-Wait像这样使用:
foreach($list in $lists){
Start-Process -FilePath $path_to_EXE -ArgumentList $list -NoNewWindow -Wait
}
Run Code Online (Sandbox Code Playgroud)
现在,我想提高优先级,我发现的一种方法是这样做:
($MyProcess = Start-Process -FilePath $path_to_EXE -ArgumentList $list -NoNewWindow -Wait -PassThru).PriorityClass = [System.Diagnostics.ProcessPriorityClass]::AboveNormal
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用,因为它需要等到进程完成,然后执行括号外的任何内容:(...).PriorityClass = ...
我不能让它继续循环并产生数千个进程,我需要一次运行一个,但是我如何告诉它提高优先级,然后等待?
您可以通过以下方式在代码中执行等待操作Wait-Process:
foreach($list in $lists) {
$MyProcess = Start-Process -FilePath $path_to_EXE -ArgumentList $list -NoNewWindow -PassThru
$MyProcess.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::AboveNormal
$Myprocess | Wait-Process
}
Run Code Online (Sandbox Code Playgroud)
从返回的对象Start-Process也有一个.WaitForExit()方法:
foreach($list in $lists) {
$MyProcess = Start-Process -FilePath $path_to_EXE -ArgumentList $list -NoNewWindow -PassThru
$MyProcess.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::AboveNormal
$Myprocess.WaitForExit()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40 次 |
| 最近记录: |