PowerShell Start-Process -Wait 不等待 VS Code

Fra*_*roe 5 powershell wait visual-studio-code powershell-core

在 PowerShell Core 中,像这样生成 VS Code,以启动差异/合并:

Start-Process -FilePath "$([System.Environment]::GetEnvironmentVariable('LOCALAPPDATA'))\\Programs\\Microsoft VS Code\\code.exe" -Wait -ArgumentList "-n --diff $FullFileNameSrcFrom $FullFileNameSrcTo"
Run Code Online (Sandbox Code Playgroud)

有效,但它不会等待。我认为这是因为 code.exe 启动子进程。如果是这种情况,我可以使用-Passthru并执行类似的操作:

Get-Process *some criteria here* | ... and check if the children processes are finished
Run Code Online (Sandbox Code Playgroud)

但是:标准是什么?

我曾经procexp试图找出它,但没有成功。

如何识别代码生成的进程?

也许有更简单的方法?

我使用 git 和 VS Code 作为默认提交编辑器,显然 git 会等待 VS Code 终止后再继续 - 所以“他们”找到了一种方法来做到这一点。这给我/你一个提示吗?

sta*_*tor 3

Start-Process -Wait还将等待子进程(来源):

使用Wait参数时,Start-Process等待进程树(进程及其所有子进程)退出,然后再返回控制权。这与 cmdlet 的行为不同Wait-Process,后者仅等待指定进程退出。

我不知道为什么,但等待并不适用于所有进程。它甚至不适用于calc.exe,但它确实适用于某些进程。

有一个解决方法可以解决您的 VS Code 问题。它本身支持等待选项:

-w--wait 等待文件关闭后再返回。

你可以像这样进行差异/合并,实际上Start-Process 在哪里等待:

Start-Process code -ArgumentList "-n -d -w .\test1.txt .\test2.txt" -Wait
Run Code Online (Sandbox Code Playgroud)

你甚至不需要Start-Process。这也将等待:

code -n -d -w .\test1.txt .\test2.txt
Run Code Online (Sandbox Code Playgroud)

  • 做得很好。至于为什么“Start-Process -Wait”在这种情况下不起作用:一旦 Visual Studio Code 运行,其“code”CLI 就会将启动其他实例委托给已经运行的实例,然后退出。正在运行的实例显然不是当前 CLI 进程的子进程。(在幕后,初始启动会产生 9 个(!)“代码”进程,每个附加窗口会再产生 3 个进程。) (2认同)