开发者

Updating a label from a thread in Perl

I'm using perl on a linux box, and I have 2 devices - a pc(the linux box) and a router/dsl-thingy - on my local net at ip addresses 192.168.1.1 & 192.168.1.2 and am trying to list or show the progress of pinging such + a test of 8 other none existing devices, with the below code, but am having troubles with my StatusLabel updating, any help...

for($i=1;$i<=10;++$i) { # --- $i<$VarClients --- 254
my $thr_List = ("ping$i");
$thr_List = threads->create(\&pingingthreads, "$i");
} 

sub pingingthreads{

     my @pingpong = ping("$localAddress$i", '-c 1', '-i .2'); # -i may not count for much?
     print "Pinging: $localAddress$i\n"; # output goes from address1 - address10 ok

     $StatusLabel = "Pinging: $localAddress$i";  # only the last responding one(device) seems开发者_开发知识库 to be shown in my statuslabel?!
     $val = ($val + 10); # 0.392156863
     print "$val\% done...\n"; # goes to 100% for me ok

    # $indicatorbar->value( $val ); # I have a ProgressBar and it gets stuck on 20% also

    if ($val == 100){$val = 0;
    } # reset after scanning
    # then after the last ping, update the statusLable:
        #my @ParamList = ('something', 'testing', 7, 8, 9);
        #$thr5 = threads->create(\&updateStatusLable, @ParamList); # starting a thread within a thread ???

# ping response text...
for( @pingpong ) { # need to do something for none responding clients & any time laps/ping latency..., or *** ???
$pong=$_; 
chop ($pong);           # Get rid of the trailling \n   ??
if ($pong =~ m/1 packets transmitted, 1 received, 0% packet loss/) {   
    push(@boxs, "$localAddress$i");
} else{
# see the other lines from the ping's output
# print "$pong\n";
}
}
}
# For $localAddress$i icmp_seq=1 Destination Host Unreachable ???

--------------------- # StatusBar/progress label & bar ----------------
my $sb = $main->StatusBar();        
$sb->addLabel( -textvariable => \$StatusLabel,
    -relief => 'flat',
    -font => $font, 
    -foreground => "$statusbartextColour",
    );


my $indicatorbar = $sb->ProgressBar( -padx=>2, -pady=>2, -borderwidth=>2,
          -troughcolor=>"$Colour2", 
        -colors=>[ 0, "$indicatorcolour" ],
          -length=>106, 
        -relief => 'flat',
        -value => "$val",
         )->pack;

  # $val = 0;
  # $indicatorbar->value( $val );

=====================================
my $StatusLabel :shared = ();
my $val :shared = (0); # var for progress bar value

I have uploaded my full code here (http://cid-99cdb89630050fff.office.live.com/browse.aspx/.Public) if needed, its in the Boxy.zip...


By default data in Perl threads are private; updates to a variable in one thread will not change the value of that variable in other threads (or in the main thread). You will want to declare $val as a shared variable. See threads::shared.

I see you have declared $val as shared at the bottom of the script, so I didn't see it until it was too late. Not coincidentally, the Perl interpreter is also not going to see that declaration until it is too late. The top 95% of your program is manipulating the global, thread-private variable $var and not the lexical, shared $var you declare at the end of your script. Move this declaration to the top of the script.

Putting use strict at the top of your program would have caught this and saved you minutes, if not hours, of grief.


You don't. GUI frameworks tend to be not threadsafe. You communicate the info to the thread in which the GUI is run instead. Example


First sorry for replying here, but have lost my cookie or the ability to reply and edit etc...

Thanks ikegami, I will have to play with the example for a while to see if I can work things out and mix it into what I'm doing... but on first sight, looks just right... Thanks very much.

I was able to update the $StatusLabel using:

# in 3 seconds maybe do a fade to: Ready...
my @ParamList = ('ping', 'testing', 4, 5, 6);
$thr2 = threads->create(\&updateStatusLable, @ParamList);

sub updateStatusLable {
# should probably check if threads are running already first???
# and if so ***/*** them ???

my @InboundParameters = @_;
my $tid = threads->tid();
# my $thr_object = threads->self(); # Get a thread's object
# print("This is a new thread\($tid\)... and I am counting to 3...\n");
sleep(3);
    $StatusLabel = "Ready..."; # print "Am now trying to change the status bar's label to \"Ready...\"\n";
# try updating better/smoother... My main window needs "focus and a mouse move" I think
    # for the new text to appear...

   # print('Recieved the parameters:  ', join(', ', @InboundParameters), ".\n" );
   # $returnedvalue = "the thread should return this...";
   # return($returnedvalue); # try returning any value just to test/see how...
}

but will try your method... Thanks again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜