Re: updating gui while running a subroutine
- From: Jeffrey Ratcliffe <jeffrey ratcliffe gmail com>
- To: Daniel Gaston <daniel gaston gmail com>
- Cc: gtk-perl-list gnome org
- Subject: Re: updating gui while running a subroutine
- Date: Fri, 30 Jan 2009 21:33:45 +0100
2009/1/30 Daniel Gaston <daniel gaston gmail com>:
Is there something special I need to do with the piped open/fork to have it
work for a subroutine within the same program code?
Below is what I have been doing - the forked code is here sub {sleep 10}.
Regards
Jeff
#!/usr/bin/perl
use warnings;
use strict;
use Gtk2 -init;
use Glib qw(TRUE FALSE); # To get TRUE and FALSE
use POSIX;
my $window = Gtk2::Window->new;
$window -> signal_connect (delete_event => \&quit);
my $pbar = Gtk2::ProgressBar->new;
$pbar -> set_pulse_step(.1);
$window -> add($pbar);
my %helperTag;
$SIG{CHLD} = \&sig_child;
my $pid = start_process(sub {sleep 10});
Glib::Timeout->add (100, sub { if (defined $helperTag{$pid}) {
$pbar->pulse;
return TRUE;
}
else {
return FALSE;
} });
$window->show_all;
Gtk2->main;
# Process the exit of the child. If you were doing something useful,
# you might keep things like information about what data needs
# to be reloaded when a child process exits.
sub sig_child {
my $pid = wait;
if ($pid >= 0) {
warn("process $pid finished\n");
delete $helperTag{$pid};
}
}
sub start_process {
my ( $process ) = @_;
my $pid = fork();
if ($pid) {
# We're still in the parent; note pid
$helperTag{$pid} = $pid;
warn("Started process $pid\n");
return $pid;
}
else {
# We're in the child. Do whatever processes we need to. We *must*
# exit this process with POSIX::_exit(...), because exit() would
# "clean up" open file handles, including our display connection,
# and merely returning from this subroutine in a separate process
# would *really* confuse things.
$process->();
POSIX::_exit(0);
}
}
# We should clean up after ourselves so that we don't
# leave dead processes flying around.
sub quit {
# 15 = SIGTERM
kill 15, $_ foreach (keys %helperTag);
Gtk2->main_quit;
}
[
Date Prev][Date Next] [
Thread Prev][Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]