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; }

In reply to Stopwatch GUI sleep by welle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.