GUI-safe polling loop



Hi all,

Just wanted to know if what I'm doing makes sense.

I'm trying to poll for a child process being finished in a way that
will not freeze the GUI up, but also won't let the surrounding code
continue until the child process is finished.

Assuming $kid is the process id of the child, is the following code
reasonable or am I missing something basic:

use POSIX qw/WNOHANG/;
require Glib;
#...
while (1) {
 last if waitpid($kid, WNOHANG); # Check if we're done
 my $ml = Glib::MainLoop->new;
 Glib::Timeout->add(2000, sub { $ml->quit; return 0 });
 $ml->run;
}

If this makes sense, I'm thinking of writing a helper function along
the lines of:

sub wait_complete {
 my ($checksub, $period, $maxwait) = @_;
 my $time = time;
 my $completed = 0;
 while (1) {
   return 1 if $checksub->(); # Completed
   return 0 if defined($maxwait) and (time - $time >= $maxwait); # Timeout
   my $ml = Glib::MainLoop->new;
   Glib::Timeout->add($period, sub { $ml->quit; return 0 });
   $ml->run;
 }
}

MB



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]