deadpickle has asked for the wisdom of the Perl Monks concerning the following question:

Continuing my Gtk2 posting series, I am trying to popup a message dialog window but the script keeps freezing and goes into a loop. I have wrote this simple script that has the same logic ( or lack of) as the large script. can anyone point out my error?
#!/usr/bin/perl -w use strict; use warnings; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; use threads; use threads::shared; my $magic: shared = 0; my $die: shared = 0; my $test = threads->new( \&test); my $window = Gtk2::Window->new( 'toplevel'); $window->set_border_width(20); $window->signal_connect('delete_event' => sub { $die = 1; $test->join; + Gtk2->main_quit; }); my $vbox = Gtk2::VBox->new( FALSE, 2); my $button = Gtk2::Button->new( "start"); $vbox->pack_start($button,FALSE,FALSE,4); $button->signal_connect( 'clicked' => sub { $magic = 1}); my $button1 = Gtk2::Button->new( "go"); $vbox->pack_start($button1,FALSE,FALSE,4); $button->signal_connect( 'clicked' => sub { $magic = 2}); $window->add( $vbox); $window->show_all; my $timer2 = Glib::Timeout->add(100, \&dialog); Gtk2->main; sub dialog { if ( $magic == 2) { # my $messagedialog_verification = Gtk2::MessageDialog->new( un +def, 'destroy-with-parent', 'question', 'yes-no', "Verify waypoints?" +); # my $response = $messagedialog_verification->run; # $messagedialog_verification->destroy; $magic = 1; } } sub test { while (1) { if ( $die == 1) {goto END}; if ($magic == 1) { for (;;) { if ( $magic == 2) { for (;;) { if ( $magic == 1) { last}; } } print "happy\n"; if ( $die == 1) {goto END}; if ( $magic == 0) { last}; } } } END: }

Replies are listed 'Best First'.
Re: Gtk2: Message dialog
by zentara (Cardinal) on Aug 03, 2007 at 10:10 UTC
    I'm not sure what you are trying to accomplish, but you are running into a basic design principle of Gtk2 timers. Timer callbacks must return a 1(TRUE) to continue, otherwise they stop (if you return 0 or FALSE).

    Your script dosn't run for me, unless I add a return 1; to the timer callback. Of course, then I see your infinite loop printing happy, which is just caused by faulty loop design as roboticus pointed out.

    sub dialog { if ( $magic == 2) { my $messagedialog_verification = Gtk2::MessageDialog->new( un +def, 'destroy-with-parent', 'question', 'yes-no', "Verify waypoints?" +); my $response = $messagedialog_verification->run; $messagedialog_verification->destroy; $magic = 1; } return 1; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Gtk2: Message dialog
by roboticus (Chancellor) on Aug 03, 2007 at 09:47 UTC

    deadpickle:

    I'm guessing it's your test subroutine. It looks like once you click the button that it will go into an infinite loop. I don't know the Gtk stuff, but I'd imagine that until test returns, you can't process the next event in the message loop. And since it looks like it's looping as fast as it can, it should make your system a bit more sluggish.

    I simplified your test routine a little just so I could see what was going on. I didn't attempt to fix any bugs though, due to my aforementioned ignorance of perl GUI programming.

    sub test { OUTER: # how is $die getting set? while ($die != 1) { # if magic != 1, we're buzzing as fast as # we can in this while loop. if ($magic == 1) { while (1) { if ( $magic == 2) { for (;;) { # buzzing in loop again... if ( $magic == 1) { last}; } } print "happy\n"; last OUTER if $die == 1; if ( $magic == 0) { last}; } } } }

    ...roboticus