Re: forked process exiting before output read
- From: "Jeffrey Ratcliffe" <jeffrey ratcliffe gmail com>
- To: zentara <zentara1 sbcglobal net>
- Cc: gtk-perl-list gnome org
- Subject: Re: forked process exiting before output read
- Date: Sun, 29 Jul 2007 00:01:18 +0200
On 27/07/07, zentara <zentara1 sbcglobal net> wrote:
If you use a thread, you can avoid this by placing the results in a shared variable.
Thanks for the tip. I have always avoided threads as I had the
impression that they don't play well with Gtk2-Perl. I have reworked
my original example with threads. Are there any obvious pitfalls with
this approach?
Jeff
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;
use Thread::Semaphore;
use Gtk2 qw/-init -threads-init/;
use Glib qw(TRUE FALSE); # To get TRUE and FALSE
my $semaphore = Thread::Semaphore->new(0);
my @queue:shared;
my $thread = threads->new(\&worker);
# Create the windows
my $window = Gtk2::Window->new('toplevel');
my $box = Gtk2::VBox->new;
my $pbar = Gtk2::ProgressBar->new;
my $buttonQuit = Gtk2::Button->new('Quit');
my $buttonStart = Gtk2::Button->new('Start');
$window->add ($box);
$box->add ($pbar);
$box->add($buttonQuit);
$box->add($buttonStart);
my %helperTag;
# We should also link this to the destroy message on the main window,
# this is just quick and dirty
$buttonQuit->signal_connect(clicked => sub{Gtk2->main_quit});
$buttonStart->signal_connect(clicked => sub{
$semaphore->up;
Glib::Timeout->add (200, sub{
if (@queue) {
my $fraction = shift @queue;
my $text = shift @queue;
$pbar->set_fraction($fraction);
$pbar->set_text($text);
if ($fraction == 1) {
$thread->join;
$semaphore->down;
return FALSE;
}
}
return TRUE;
});
});
$window->show_all;
Gtk2->main;
sub worker {
$semaphore->down;
my $n = 4;
for (my $i = 0; $i <= $n; $i++) {
sleep(1);
push @queue, $i/$n;
push @queue, "Running $i of $n";
}
$semaphore->up;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]