2011-09-02 32 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... 
} 

但將再次嘗試你的方法...謝謝。