No they are scheduled in the Tk eventloop, it is kind of a time-slicing, see Tk event loop explained. You could use the modo example in that page, to reschedule a timer when it finishes, but shift in different data from an array or something.
Is Perl a thread language and if not, what is the Thread::Semaphore module for?
Thread::Semaphore works fine with Perl, and Perl can be built to be threaded, but it builds itself as non-threaded as the standard configure options. Also, just because the Perl interpreter is threaded, it does not mean ALL modules are threadsafe. The interpreter has no idea of what's in the modules you load in, and you have to specifically check the docs or google, to see if your module is threadsafe. Non threadsafe modules tend to use globals that cross thread boundaries and confuse the interpreter.
The problem you are running into, is that Tk IS NOT threadsafe. You can google for numerous discussions of how to run Tk with threads. FWIW, Gtk2 has better thread safety, and you can put a GLib timer in a thread and use Glib->idle_add to have it safely do things in the main gui.
how to control the execution of the after commands so they do not over lap
Either set them up to run sequentially, or use a flag of somesort, to launch (or prevent launching) of the next timer. Using the modo example
my @data = qw( 1 2 3 4 5); modo(); sub modo { # Do stuff, then reschedule myself. my $newdata = shift @data; if ( defined $newdata){ $MW->after($MILLISECOND_DELAY, [ \&modo, $newdata ] ); } }
If you want them running concurrently, in separate threads, switch to Gtk2, or more specifically Glib , the base class for Gtk2, with no gui. Or may POE?
Here is an example of using Glib timers and threads. This particular example dosn't put timers in the threads, but you can do it. It does demonstrate how to use Glib->idle_add in threads.
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Glib qw/TRUE FALSE/; #setup shared hash my %shash; #share(%shash); #will work only for first level keys my $numworkers = 3; foreach my $dthread(1..$numworkers){ share ($shash{$dthread}{'go'}); share ($shash{$dthread}{'work'}); share ($shash{$dthread}{'die'}); share ($shash{$dthread}{'work'}); $shash{$dthread}{'go'} = 0; $shash{$dthread}{'work'} = ''; $shash{$dthread}{'die'} = 0; $shash{$dthread}{'work'} = 0; } my $finished = 0; my $main_loop = Glib::MainLoop->new; my $x = 0; my $y = 0; #create 3 sleeping thread passing it info for my $num(1..3){ my $thread = threads->new(\&work,$num, $x, $y); } #start threads $shash{1}{'go'} = 1; $shash{2}{'go'} = 1; $shash{3}{'go'} = 1; #timer to watch for threads finishing my $timer1 = Glib::Timeout->add (100, \&timer1_callback, undef, 1 ); sub timer1_callback{ if( $shash{1}{'work'} == 10 and $shash{2}{'work'} == 10 and $shash{3}{'work'} == 10 ){ print "\t\t\t\t\tFINISHED!!\n"; clean_exit(); # return 0; #stop timer }else{ return 1;} #keep timer going } # do something in parent thread my $count = 1; my $timer = Glib::Timeout->add (10, \&timer_callback, undef, 1 ); sub timer_callback{ $count++; print "main $count\n"; if($finished){return 0}else{return 1} } $main_loop->run; sub clean_exit{ $shash{1}{'die'} = 1; $shash{2}{'die'} = 1; $shash{3}{'die'} = 1; exit; } sub work{ my ($num,$x,$y) = @_; $|++; my $pstring; for(1..$num){$pstring .= "\t"} foreach my $z (1..10){ Glib::Idle->add( sub{ if($shash{$num}{'die'} == 1){ return }; print "$pstring thread.$num->$z\n"; $shash{$num}{'work'} = $z; return FALSE; }); select(undef,undef,undef, .1); } }
In reply to Re: Perl TK and after cmd
by zentara
in thread Perl TK and after cmd
by brian42miller
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |