Hi, here is about as simple an example as you can get. It uses a thread to watch a filehandle(like tail), push the results into a shared array, then display the results in a Tk::Text box. It dosn't get much simpler than this. Basically the shared array is a buffer between the thread and main Tk code. NOT thorougly tested for buffer problems.
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; # must setup thread code before any Tk code is used # to avoid Tk thread-safety problems my @logdata : shared; my $thread_die : shared; @logdata = (); $thread_die = 0; my $thread = threads->new( \&work ); ############################################ use Tk; my $mw = MainWindow->new(-background => 'gray50'); my $tframe = $mw->Frame( -background => 'gray50',-borderwidth=>10 ) ->pack(-side =>'top' ,-fill=>'y'); my $bframe = $mw->Frame( -background => 'gray50',-borderwidth=>10 ) ->pack(-side =>'bottom',-fill =>'both' ); my $text = $tframe->Scrolled("Text", -scrollbars => 'ose', -background => 'black', -foreground => 'lightskyblue', )->pack(-side =>'top', -anchor =>'n'); my $exit_button = $mw->Button(-text => 'Exit', -command => sub{ $thread_die = 1; #kill thread $thread->join; exit; })->pack(); my $timer = $mw->repeat(1000, sub{ lock( @logdata ); #locks within scope my @in = @logdata; #copy it @logdata = (); #clear out old log lines $text->insert('end', "@in"); $text->see('end'); }); MainLoop; ######################################## sub work{ $|++; open(FH,"< z.log") or die "$!\n"; while(1){ while(<FH>){ push @logdata, $_; if( $thread_die == 1 ){return} #kill thread } } } ############################################

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to Re^2: Tk-with-worker-threads by zentara
in thread Tk-with-worker-threads by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.