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

Dear monks,

I have a simple gui. Two buttons: "start" counting and "end" counting. Clicking on "start" , the script starts counting and prints out. I would like to be able to press anytime the button "stop" and stop the counting. While counting the Window is frozen.

#!/usr/bin/perl -w use strict; use warnings; use Tk; my $mw = MainWindow->new(); $mw->Button( -text => 'Start', -command => \&start_process )->pack; $mw->Button( -text => 'Stop', -command => \&stop_process )->pack; MainLoop; sub start_process{ my $count = 0; while ($count < 10) { print $count; sleep 1; $count ++; } } sub stop_process{ #stopping the process }

Even putting a $mw->update(); in the while loop (as suggested in other posts) doesn't change the behaviour. What is the best practice to keep the Window "alive" even while counting?

Thanks, cla

Replies are listed 'Best First'.
Re: stop counting (process)
by Anonymous Monk on Aug 25, 2010 at 01:11 UTC
    Hi fanticla,

    While it's true that $mw->update() should provide a little relief from a frozen gui:

    sub start_process{ my $count = 0; while ($count < 10) { $mw->update(); print $count; sleep 1; $count ++; } }

    your code will still exhibit some sluggishness because of the sleep 1 statement, during which nothing can update.

    You can fix that quite easily by using select in place of sleep, like this:

    sub start_process{ my $count = 0; while ($count < 10) { print $count; # sleep 1; for (my $i = 0; $i < 10; $i++) { select(undef, undef, undef, 0.1); $mw->update; } $count ++; } }
    which is effectively a sleep 0.1, performed 10 times (with the update happening during each iteration of the loop).

    Of course, you can still press the button while the subroutine is being called.  If you'd like to only be able to call start_process if that subroutine is not currently in the process of running, you could add a boolean variable at the top of your code:

    my $b_started = 0;
    and then test it in the subroutine:
    sub start_process{ $b_started++ and return; my $count = 0; while ($count < 10) { print $count; # sleep 1; for (my $i = 0; $i < 10; $i++) { select(undef, undef, undef, 0.1); $mw->update; } $count ++; } $b_started = 0; }
      Or Time::HiRes (Time::HiRes::sleep(0.10);), if using Perl version 5.7.3 or greater.
Re: stop counting (process)
by zentara (Cardinal) on Aug 25, 2010 at 11:39 UTC
    Never use sleep() in a Tk (or any gui ) unless it is in it's own thread. Here is how I would do it, use a timer.
    #!/usr/bin/perl -w use strict; use warnings; use Tk; my $timer; my $mw = MainWindow->new(); $mw->Button( -text => 'Start', -command => \&start_process )->pack; $mw->Button( -text => 'Stop', -command => \&stop_process )->pack; MainLoop; sub start_process{ my $count = 0; $timer = $mw->repeat(1000, sub{ if ( $count < 10 ){ $count++ ; print "$count\n";} }); } sub stop_process{ #stopping the process $timer->cancel; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      The examples work fantastically

      Thank you all

Re: stop counting (process)
by JavaFan (Canon) on Aug 24, 2010 at 23:00 UTC
    Use the after or repeat methods. Callable from every widget.