At present I am working on GUI in Perl/Tk. In this GUI I created some image icon Buttons. And I want to make this buttons as Multi-Threaded. For one of the button's subroutine I created a thread. I created thread with all the rules ( I shared the variables that need work with thread, created thread at begininng). But I am facing a problem in getting this thread worked. The problem in the threaded subroutine is I am loading a external perl file( for example - do 'add.pl') which is not working ( i mean not loading). The external file also loads other files, modules in itself. What is the problem I am facing here? Is that I have make all the variables in this external file as shared variables for thread? The part of the code is given below:
use threads; use threads::shared; my $KillThread : shared; my $FunctionName : shared; my $ThreadWork : shared; my @ArgumentsThread : shared; my $RefResultFunction : shared; $ThreadWork = 0; $KillThread = 0; my %FunctionsToLaunchThread = ( "run_script" =>\&run_script ); # Creation of thread my $Thread = threads->create( \&WorkThread ); sub load { ......some code...... $run_button->configure(-command =>[ (\&runtk, $paste_text)],-state => +'normal'); ......some code.... } sub runtk { my $temp = $_[0], $FunctionName = "run_script"; @ArgumentsThread = ($temp); $ThreadWork = 1; $run_button->configure(-state => "disabled"); while ( $ThreadWork == 1 ) { sleep 0.2; $f->update; } $run_button->configure(-state => "normal"); } sub run_script{ open (MYFILE1, "subroutines.txt"); my @sub_lines = <MYFILE1>; my $current = localtime(time); #print "Current date and time is $current"; use POSIX qw(strftime); my $now_string = strftime "%d-%b-%Y--%T", localtime; print $now_string; my $str ="$now_string.txt"; print $str; open FILE, "+>>Test_Results/$str" or die $!; do "add.pl"; use Device::Modem::GSM; my $modem = new Device::Modem::GSM( port => "/dev/$port_global",log = +> "file,$now_string.log", loglevel => +'debug', # default is 'warning' + ); sub WorkThread { while (1) { if ( $ThreadWork == 1 ) { $FunctionsToLaunchThread{$FunctionName}->@ArgumentsThread); $ThreadWork = 0; } last if ( $KillThread == 1 ); sleep 0.5; } return; }

In reply to Perl/Tk Multithreading by mahis_431

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.