为什么 Process.GetProcessesByName("msiexec") 显示错误的结果?
Why the Process.GetProcessesByName("msiexec") shows wrong result?
有时我需要捕获 msiexec
进程启动和完成的事件。我的代码使用通常的用户权限。 msiexec
进程将由 Windows 以系统权限启动,因此我无法执行 Refresh()
并检查该进程的 HasExited
属性。
while (true) {
Process[] msi = Process
.GetProcessesByName("msiexec");
if (msi.Length > 0) break;
}
// The `msiexec` is launched. Now to wait its finishing...
while (true) {
Process[] msi = Process
.GetProcessesByName("msiexec");
if (msi.Length == 0) break; // Here is `false` always!
}
// Components was installed.
但是第二个 while
循环总是显示 msi.Length == 1
,即使在 msiexec
完成之后也是如此。为什么会这样?我该如何解决?
请注意,多个同名进程可以同时 运行。因此,msi.Length
可以 > 1。在启动 msiexec 之前它也可以 > 0。
找到 msiexec 进程后,您可以测试进程的 HasExited
属性,而不是再次调用 GetProcessesByName
。
如果 msi.Length
> 1,则等待未退出进程数减 1。
有时我需要捕获 msiexec
进程启动和完成的事件。我的代码使用通常的用户权限。 msiexec
进程将由 Windows 以系统权限启动,因此我无法执行 Refresh()
并检查该进程的 HasExited
属性。
while (true) {
Process[] msi = Process
.GetProcessesByName("msiexec");
if (msi.Length > 0) break;
}
// The `msiexec` is launched. Now to wait its finishing...
while (true) {
Process[] msi = Process
.GetProcessesByName("msiexec");
if (msi.Length == 0) break; // Here is `false` always!
}
// Components was installed.
但是第二个 while
循环总是显示 msi.Length == 1
,即使在 msiexec
完成之后也是如此。为什么会这样?我该如何解决?
请注意,多个同名进程可以同时 运行。因此,msi.Length
可以 > 1。在启动 msiexec 之前它也可以 > 0。
找到 msiexec 进程后,您可以测试进程的 HasExited
属性,而不是再次调用 GetProcessesByName
。
如果 msi.Length
> 1,则等待未退出进程数减 1。