Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Run subroutine occasionally

by Marshall (Canon)
on Mar 01, 2022 at 00:11 UTC ( [id://11141722]=note: print w/replies, xml ) Need Help??


in reply to Run subroutine occasionally

consider: $MW->repeat($MILLISECOND_DELAY, \&someSub);
runs someSub repeatedly.

Of course, sub someSub{} should run "quickly" or the GUI will "freeze".

Update: I re-read the question and perhaps I made an incorrect about this being a Tk GUI? If so, the OP needs to clarify.

Update with Code for a GUI:

use strict; use warnings; use Tk; # quick hack # reports the last function button pressed # every 3 seconds # Doesn't start reporting until first button is pressed. my @buttons = (["function1", \&func1], ["functoin2", \&func2], ["function3", \&func3],); my $last_function_executed =0; sub func1{ print "this is function 1\n"; $last_function_executed = 1; } sub func2{ print "this is function 2\n"; $last_function_executed = 2; } sub func3{ print "this is function 3\n"; $last_function_executed = 3; } sub report_last_func{ return unless $last_function_executed; # no button pressed yet! print "most recent operation was: $last_function_executed\n"; } my $MW = MainWindow->new(); foreach my $button_ref (@buttons) # create buttons { my ($text,$subref) = @$button_ref; $MW->Button(-text=> $text, -command => $subref)-> pack; } $MW->repeat(3*1000, \&report_last_func); MainLoop;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found