简体   繁体   中英

How can I have a Perl/Tk progress bar showing that an external process is running?

I am creating a Perl/TK GUI code that will call a seperate exe inside. The progress bar widget will be the only indication that execution is happening inside it but problem is, as you run the code, the progress bar freezes because it has to finish first the execution of the seperate exe and after it's done, activity on the progress can be updated.

Is there a better way to have a simultenous implementation of the progress with respect to the seperate exe so as to have the real time execution of the code?

How are you calling the external program? If you are blocking on something like system() , don't do that. Run the program in a separate process then poll to check if it is running. As long as it is running, you update your progress indicator.

Do you get output for progress from the other process? if so, you can use open() to run your subprocess.

open(MYSUBPROC, '-|', "myprocess args") or die "could not execute!";
while (<MYSUBPROC>)
{
    #do something...
}
close(MYSUBPROC) || warn "subproc returned error $?";

You should also take a look at the perlipc sections on using open() for IPC , and Safe pipe opens

I don't know enough Perl/Tk to whip up a quick example.

worker.pl

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

for (0 .. 10) {
    print 10 * $_, "\n";
    sleep 1;
}

boss.pl

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

open my $worker_h, '-|', 'worker.pl'
    or die "Cannot open pipe to worker: $!";

while ( my $status = <$worker_h> ) {
    chomp $status;
    my $ticks = 2 * ($status/10);
    print '=' x $ticks, "\r" x $ticks;
    # do other stuff;
}

close $worker_h
    or die "Error closing pipe to worker: $?";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM