使用Perl轮询进程退出

时间:2012-01-31 05:54:16

标签: windows perl

我正在使用Perl自动执行一些安装过程。现在我想知道我解雇的安装程序何时完成。我该怎么做呢?由于这是自动化工作,我不能要求人们在以后的某个时间点发出一些命令。此功能应该是自动的。我如何在Windows上执行此操作?

2 个答案:

答案 0 :(得分:4)

在Windows上,使用进程句柄并调用WaitForSingleObject()以找出进程何时终止。如果您只有进程ID,则可以使用OpenProcess()来获取进程ID。 (当然,如果你自己使用CreateProcess()创建了这个过程,那么你已经掌握了它。)

答案 1 :(得分:3)

Greg Hewgill的答案解决了您需要的基础Windows API函数,但没有解决如何在Perl中使用它们的问题。您可以使用Win32::Process模块:

use strict;
use warnings;

use Win32::Process;

Win32::Process::Create(
  my $process,
  'C:\WINDOWS\system32\notepad.exe', # path of executable
  "notepad",                         # command line it sees
  0,                                 # don't inherit our handles
  NORMAL_PRIORITY_CLASS,             # process creation flags
  "."                                # current directory for process
) or die $^E;

print "started\n";

$process->Wait(INFINITE);

print "done\n";

$process->GetExitCode(my $exitcode) or die $^E;

print "process exit code $exitcode\n";
如果您需要一次等待多个对象,

$process也可以传递给Win32::IPC函数wait_anywait_all

相关问题