in reply to Sharing Tk-module objects in threads

Try something like this. Making the lists scroll and a proper way to terminate the worker thread(s) is something gor you to read up on.

use strict; use Tk; use Threads; use Thread::Queue; sub create_tk_window { my $mw=MainWindow->new ( -background=>'#dedede', -foreground=>'yellow', -title=>"FingerLick" ); $mw->geometry("802x618"); $mw->minsize(802,618); $mw->maxsize(802,618); my $sent_recvd_listbox=$mw->Listbox ( -height=>10, -width=>60, -background=>'black', -foreground=>'yellow' )->pack(-side=>'bottom',-anchor=>'s',-pady=>2); my $server_list_listbox=$mw->Scrolled ( "Listbox", -height=>20, -width=>60, -background=>'white', -foreground=>'black', -scrollbars=>'se', )->pack(); return( $mw, $sent_recvd_listbox, $server_list_listbox ); } sub update_thread { my( $Qservers, $Qxmit ) = @_; while(1) { $Qservers->enqueue( "Server" . int rand 1000 ) if rand() < .1; $Qxmit->enqueue( "Sent: la la la " . int rand 1000 ) if rand() + < .1; $Qxmit->enqueue( "Recv: do be do be do " . int rand 1000 ) if +rand() < .1; select undef, undef, undef, 0.1; } } my $Qservers = new Thread::Queue; my $Qxmit = new Thread::Queue; my( $mw, $srl, $slb ) = create_tk_window(); sub updateScreen { $slb->insert( 'end', $Qservers->dequeue ) if $Qservers->pending; + $srl->insert( 'end', $Qxmit->dequeue ) if $Qxmit->pending; } threads->new( \&update_thread, $Qservers, $Qxmit )->detach; $mw->repeat( 100, \&updateScreen ); $mw->MainLoop;

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Sharing Tk-module objects in threads
by kabeldag (Hermit) on Nov 05, 2004 at 05:11 UTC
    BrowserUk, you bloody champ you !!
    Guess what, I found out a way aswell ! Using IO::Pipe.

    I figured out the following way to achieve my goal but you beat me to it.. L: hehe. And you know what, It's the repeat Tk method aswell o_(.
    PLUS, I am using fork again. When I said I wasn't going to. But your way is very nice.
    use IO::Pipe; use Tk; $pipe = new IO::Pipe; create_tk_window(); if($pid = fork()) { # Parent $pipe->reader(); $mw->MainLoop; }elsif(defined $pid) { # Child $pipe->writer(); while(1) { write_to_pipe(); } } sub write_to_pipe { $pipe->write("Who's a jebroni - Not me Not me"); } sub display_handle_data { my $pipe_data; $pipe->sysread($pipe_data,100024); $server_list_listbox->insert('end',"GOT $pipe_data"); $server_list_listbox->see('end'); } sub create_tk_window { $mw=MainWindow->new ( -background=>'#dedede', -foreground=>'yellow', -title=>"FingerLick - Only eye nows the gows wehn the wind blo +ws" ); $mw->geometry("802x618"); $mw->minsize(802,618); $mw->maxsize(802,618); $mw->repeat(1.001,sub { display_handle_data() }); $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(); }
    The Threads::Queue method that you have suggested may be the winner I think.
    We shall see after some tests...etc..

    Thank you very much BrowserUk. Now things are moving forward a lil more...
    I wanted to finish this thing by this weekend, looks like I may now.

    Thanks alot mate : _)