welle has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks
What I'd like: Bind time (clock) events to my program. Let's take this very simple (and poor) program. It's a basic stopwatch (only seconds). Clicking on Start will start counting, clicking on Stop...of course it stops. Counting is the 1st (and only) process. The GUI can be used thanks windows->update. Yet, this makes the app not responsive. If I had sleep 10 (instead of 1), the GUI would respond only every 10 seconds (always updating needs to many resources?). I'd like it to be any time ready to respond.
Now, what is the best approach to solve this? I've no experience with fork. Is fork the right way? Any suggestion would be very appreciated
#!/usr/bin/perl use Tk; use strict; my $count=0; my $window = MainWindow->new; $window->title("My Example"); $window->Label(-textvariable => \$count )->pack; $window->Button(-text => "Start", -command => \&start )->pack; $window->Button(-text => "Stop", -command => \&stop )->pack; MainLoop; ######################################################### my $stop; sub start { while ($count <= 10) { $window->update; $count ++; sleep 1; if ($stop =~ 1) { $stop=0; $count=0; return; } } } sub stop{ $stop=1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Stopwatch GUI sleep
by Corion (Patriarch) on Apr 07, 2011 at 07:32 UTC | |
|
Re: Stopwatch GUI sleep
by wind (Priest) on Apr 07, 2011 at 21:28 UTC | |
by welle (Beadle) on Apr 08, 2011 at 14:58 UTC |