Perl:信号和线程.如何使用qx()杀死线程



我有一个脚本,可以解析日志并查找错误和警告
我想使用用户友好的解释这个日志
因此,我使用notepad
这是代码:

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)=@ARGV;
#open my $ih, "<", $input or die "can't open input file with logn";
open my $oh, ">", $output or die "can't open output file with logn";
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 goodn";};
                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(<>);

当我试图杀死运行记事本的线程时,它掉了下来
如何使用信号和线程做到这一点?有可能吗
以及您对解决方案的想法
谢谢

这不起作用,因为您的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或某种共享变量来决定是否要杀死你的记事本。

最新更新