2011-09-02 30 views
0

我在linux上使用perl,我有2个设备 - 一个pc(linux盒子)和一个路由器/ dsl-thingy - 在我的本地网上ip地址192.168 .1.1 & 192.168.1.2并试图列出或显示ping的进度这样的+其他没有现有设备的测试,下面的代码,但我有我的StatusLabel更新的麻烦,任何帮助...从Perl中的线程更新标签

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 

我在这里上传我的全部代码(http://cid-99cdb89630050fff.office.live.com/browse.aspx/.Public)如果需要的话,其在Boxy.zip ...

+0

欢迎来到SO。 SO不是一个代码工厂。你需要提出具体的问题,你会得到具体的答案。 – musiKk

+0

对不起,如果上述不正确,我的问题不同的是......我如何更新Perl中的线程标签。我正在使用上述只是为了显示我在哪里(只是在玩)。 – Carpenter

回答

0

你不知道。 GUI框架往往不是线程安全的。您将信息传递给运行GUI的线程。 Example

3

默认Perl线程中的数据是 private;更新一个线程中的变量不会更改其他线程(或主线程)中该变量的值。您将要声明 $val作为共享变量。 见 threads::shared

我看到你已经在脚本的底部声明了$val,所以直到为时已晚,我才看到它。不是巧合的是,Perl解释器也不会看到这个声明,直到为时已晚。您的程序的前95%是操纵全局线程专用变量$var,而不是您在脚本结尾处声明的词汇共享$var。将此声明移至脚本的顶部。

use strict放在你的程序的顶部会抓住这个并且节省你几分钟的时间,如果不是几小时的话。

+0

+1用于严格使用。 – snap

0

首先对不起回复这里,但已经失去了我的cookie或回复和编辑等能力......

感谢池上,我会用例子来玩了一会儿,看看我是否能解决问题并将其融入我正在做的事情中......但一见钟情,看起来恰到好处......非常感谢。

我能够更新$ StatusLabel使用:

# 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... 
} 

但将再次尝试你的方法...谢谢。