Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

TK Timed action

by randor (Novice)
on Sep 25, 2012 at 12:54 UTC ( [id://995541]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I am working on a Perl::TK project and I am trying to figure out the theory behind making a timed process run off.

issue:
I connect to a database and pull data from it and display it on the screen. I would like to have the program check for updates every 30 seconds or so and if need be, update the data.

Problem:
I don't really have a good grasp as to how to efficiently force the program to check every x seconds?

My Idea:
I tried to do a check time statement in the main body of the script like so:

if(time - $StartTime) > 30) { &RefreshData; $StartTime = time;}
and have the program set the time initially when it first starts the section. But this did not work, I did some testing and even though I placed the if statement in the main body (just above the mainloop) it did not get called, which makes me believe that the system does not keep cycling (ie, like Java) but instead just sits and waits?

so my question is can someone help me with the theory of how to make this work? I assume I am missing something? I have been working on this for the last day and I am stumped :(

Thank you for any help,
Bob

Replies are listed 'Best First'.
Re: TK Timed action
by zentara (Archbishop) on Sep 25, 2012 at 15:30 UTC
    I don't really have a good grasp as to how to efficiently force the program to check every x seconds?
    ......
    It turns out that $mw->after(1000, \&sub); works.. then just add that into the sub to repeat..

    Hi, Tk::after is usually used for a one-shot delay. What you want is Tk::repeat. Try

    my $repeater = $mw->repeat(1000, \&sub); .... $repeater->cancel; # when you want to stop it
    and you will not need to repeatedly call Tk::after in your sub.

    Here is a basic example:

    #!/usr/bin/perl use Tk; use warnings; use strict; my $mw = MainWindow->new(title => "Timer"); my $elapsed_sec = 0; my $elapsed_sec_label = $mw->Label(-textvariable => \$elapsed_sec)->pa +ck(); my $repeater; # declare first so it can be accessed in the callback $mw->Button(-text => "reset", -command => sub { $elapsed_sec = 0; &repeater(); })->pack(); $mw->Button(-text => "exit", -command => sub { exit })->pack(); # start first run &repeater(); MainLoop; sub repeater{ #this is repeated every second, and you can put your is_playing here $repeater = $mw->repeat(1000 => sub { $elapsed_sec ++; if ($elapsed_sec > 4){ $repeater->cancel } } ); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: TK Timed action
by randor (Novice) on Sep 25, 2012 at 13:44 UTC
    ok, so after FURTHER studies, I finally solved this equation..

    It turns out that $mw->after(1000, \&sub); works.. then just add that into the sub to repeat..

    Bob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://995541]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-19 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found