Running the script above ^^ for me on a windows 2000 machine with v5.8.3, the update_thread (obviously) will just continue printing "Couldn't insert any data cause this thread can't see the tk objects !?" until you interrupt it.use strict; use Tk; use Threads; my ($mw,$sent_recvd_listbox,$server_list_listbox); threads->new(\&update_thread)->detach; create_tk_window(); $mw->MainLoop; sub update_thread { print "update_thread called...\n"; while(1) { if(defined($server_list_listbox)) { # This never gets executed because the objects from # the tk module are out of scope ?????? $server_list_listbox->insert('end',"Some string data"); }else{ print "Couldn't insert any data cause this thread can't se +e the tk objects !?\n"; } sleep 1; } exit(); } sub create_tk_window { $mw=MainWindow->new ( -background=>'#dedede', -foreground=>'yellow', -title=>"FingerLick" ); $mw->geometry("802x618"); $mw->minsize(802,618); $mw->maxsize(802,618); $sent_recvd_listbox=$mw->Listbox ( -height=>1, -width=>60, -background=>'black', -foreground=>'yellow' )->pack(-side=>'bottom',-anchor=>'s',-pady=>2); $server_list_listbox=$mw->Scrolled ( "Listbox", -height=>20, -width=>60, -background=>'white', -foreground=>'black', -scrollbars=>'se', )->pack(); }
Then, in the display_worker_data() sub, check some shared scalar that relates to what data the worker_thread has to offer, and just as importantly, where it has to go (what Tk widget field).my $wt_display_code:shared; my $wt_data:shared; $mw->repeat(0.001,sub { display_worker_data() });
BUT THAT IS A NIGHTMARE ^^ The number of possible value pairs are endless.my $wt_display_code:shared; my $wt_data:shared; $mw->repeat(0.001,sub { display_worker_data() }); sub display_worker_data() { if($wt_display_code eq "SENT_RECVD") { my $data_for_display="Sent recvd: $wt_data"; $sent_recvd_listbox->insert('end',$data_for_display"); }elsif($wt_display_code eq "TIME_STATUS") { my $data_for_display="Time: $local_time"; $time_stats_listbox->insert('end',"$data_for_display"); }elsif(...) { ... }elsif(...) { ... } etc... }
Maybe I should be using a simple pipe to communicate data between threads. Don't tell me to use fork. Because it gave me more greif than you can poke a stick at, thats why I am trying to use threads.
Janitored by Arunbear - added readmore tags, as per Monastery guidelines
In reply to Sharing Tk-module objects in threads by kabeldag
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |