Re: non-blocking loop delay
- From: zentara <zentara zentara net>
- To: Gtk Perl List <gtk-perl-list gnome org>
- Subject: Re: non-blocking loop delay
- Date: Mon, 13 Jun 2005 16:30:16 -0400
Thanks muppet, your technique for suspending the mainloop
works just like I wanted.
sub non_blocking_delay {
my $milliseconds = shift;
my $mainloop = Glib::MainLoop->new;
Glib::Timeout->add ($milliseconds, sub { $mainloop->quit;
FALSE; });
$mainloop->run;
}
I was responding to someone offlist trying to make a pulse-progressbar example
work in a situation where a series of subroutines were to be run sequentially.
I was using a simple counter as the test subs, but they went too fast. When I tried
the conventional Timeout, they ran in parallel. I resorted to just counting to a very high
number, which of course just max out the cpu.
In case anyone is interested, this is how I actually used your non-blocking-delay sub.
I was using the horrible technique in process_b to acheive the result, but
it maxed cpu. Thanks again, I will always remember this sub.
#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';
#this is a sequential processor
my $wait_dialog = 0; #just a global flag to track wait popup
my $window = Gtk2::Window->new('toplevel');
$window->set_title('Z');
$window ->signal_connect( 'destroy' => \&delete_event );
$window->set_border_width(10);
$window->set_size_request(300,200);
my $vbox = Gtk2::VBox->new( FALSE, 6 );
$window->add($vbox);
$vbox->set_border_width(2);
my $hbox= Gtk2::HBox->new( FALSE, 6 );
$vbox->pack_end($hbox,FALSE,FALSE,0);
$hbox->set_border_width(2);
$vbox->pack_end (Gtk2::HSeparator->new, FALSE, FALSE, 0);
my $button = Gtk2::Button->new_from_stock('gtk-quit');
$hbox->pack_end( $button, FALSE, FALSE, 0 );
$button->signal_connect( clicked => \&delete_event );
my $button1 = Gtk2::Button->new('Init Dialog');
$hbox->pack_end( $button1, FALSE, FALSE, 0 );
$button1->signal_connect( clicked => \&initialize );
$window->show_all();
Gtk2->main;
#####################################
sub delete_event {
Gtk2->main_quit;
return FALSE;
}
#######################################
sub initialize{
$window->window->set_cursor(Gtk2::Gdk::Cursor -> new("watch"));
$wait_dialog = 1; #set the flag
&display_popup; #display the popup
&run_stuff;
}
#################################################
sub run_stuff{
&start_process_a;
&start_process_b;
&start_process_c;
}
##################################################
sub display_popup {
#create the 'waiting' dialog
my $popup = Gtk2::Window->new('popup');
$popup -> set_resizable (FALSE);
$popup->set_position('center');
my $pbar = Gtk2::ProgressBar->new();
$pbar->set_pulse_step(.1);
my $vbox0 = Gtk2::VBox->new (FALSE, 6);
$vbox0->set_border_width(10);
my $label=Gtk2::Label->new ();
$label->set_justify('GTK_JUSTIFY_CENTER');
$label->set_markup("<b>".("Please wait... loading data...")."</b>");
$vbox0->pack_start($label,FALSE, FALSE, 0);
$vbox0->pack_start($pbar,FALSE, FALSE, 0);
$pbar->show;
$vbox0->show_all;
$popup->add($vbox0);
$popup->show_all;
my $timer = Glib::Timeout->add (100, sub{
if($wait_dialog == 1){
$pbar->pulse;
return TRUE;
}else{
$window->window->set_cursor(Gtk2::Gdk::Cursor -> new("left-ptr"));
$popup->destroy;
return FALSE;
}
} );
}
#######################################
sub start_process_a{
print "In a\n";
my $count = 0;
for(1..5){
$count++;
print "$count-a\n";
non_blocking_delay(1000);
}
}
sub start_process_b{
print "In b\n";
my $count = 0;
for(1..300000){
$count++;
# print "$count-b\n";
Gtk2->main_iteration while Gtk2->events_pending;
}
}
sub start_process_c{
print "In c\n";
$wait_dialog = 0; #set flag to destroy popup
}
sub non_blocking_delay {
my $milliseconds = shift;
my $mainloop = Glib::MainLoop->new;
Glib::Timeout->add ($milliseconds, sub { $mainloop->quit; FALSE; });
$mainloop->run;
}
__END__
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]