2012-07-31 43 views
2

我有一个脚本,解析日志并查找错误和警告。
我想用这个日志的用户友好的解释。
因此,我使用notepad
这里是代码:Perl:信号和线程。如何用qx()杀死线程

use v5.16; 
use strict; 
use warnings; 

use Win32::Clipboard; 
use threads; 
use utf8; 

my $kp = Win32::Clipboard->new(); 

my $output = shift || "out_log.txt"; 

#my ($input, $output)[email protected]; 
#open my $ih, "<", $input or die "can't open input file with log\n"; 

open my $oh, ">", $output or die "can't open output file with log\n"; 
my @alls=split /\n/,$kp->Get(); 

for my $k(0..$#alls){ 
    $_ = $alls[$k]; 
    if(/^ERR.+|^WARN.+/){ 
     print {$oh} qq(at position $k -->).$_."\n"; 
     } 
    } 
my $thread = 
threads->create(sub{ 
       $SIG{INT}=sub{die"All good\n";}; 
       qx(notepad $output); 
      } 
     ); 

print qq(type 'y' for quit); 
do{ 
    print "want to quit?\n>" ; 
    chomp; 
    do{ 
     say "I will kill this thread"; 
     $thread->kill('INT') if defined($thread);  
     say "and delete output"; 
     unlink $output; 
     exit(0); 
     }if (m/y/); 
    }while(<>); 

摔倒,当我试图杀死其运行记事本线程。
如何做到这一点,使用信号和线程?可能吗?
和你的解决方案的想法,请。
谢谢!

回答

1

这不起作用,因为您的SIGINT永远不会传递给notepad。所以它永远不会关闭。 (和那个处理程序 - 可能永远不会得到处理)。

您需要采取不同的方法。查看Win32::Process了解如何产生/杀死记事本进程的一些示例。

my $ProcessObj; 
    Win32::Process::Create($ProcessObj, 
     "C:\\Windows\\system32\\notepad.exe", 
     "notepad", 0, NORMAL_PRIORITY_CLASS, ".") 
     or die $!; 

然后你就可以使用

$ProcessObj -> Kill(1); 

我使用Thread::Semaphore或某种共享变量来决定,如果你想杀死你的记事本建议。