【问题标题】:Polling for process exit using Perl使用 Perl 轮询进程退出
【发布时间】:2012-01-31 17:58:21
【问题描述】:

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

【问题讨论】:

  • 问题是,你到底在等什么?一个完成的过程,也许?如果有,你知道进程的名称或 pid 吗?
  • @GregHewgill 我在 windows 上工作,所以你.. 我会等待一个进程完成.. 你能告诉我我必须如何检查 windows 上的进程终止。

标签: windows perl


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多