2011-09-02 5 views
0

나는 리눅스 박스에서 perl을 사용하고 있으며, IP 주소 192.168에서 로컬 네 트워크에 pc (리눅스 박스)와 라우터/dsl-thingy라는 2 개의 장치가있다. .1.1 & 192.168.1.2 그리고 아래의 코드와 함께 8 개의 다른 none none 디바이스의 테스트와 같은 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는 코드 팩토리가 아닙니다. 구체적인 질문을해야하며 구체적인 답변을 얻을 수 있습니다. – musiKk

+0

위의 경우 옳지 않다면 죄송합니다, 내 질문에 달리 다 ... Perl 스레드에서 레이블을 어떻게 업데이트합니까. 나는 위의 그림을 사용하여 내가 어디에 있는지를 보여주고있다. – Carpenter

답변

0

그렇지 않습니다. GUI 프레임 워크는 스레드 세이프하지 않는 경향이 있습니다. 대신 GUI가 실행되는 스레드에 정보를 전달합니다. 내가 할 수있는 경우

가 가 이케 가미, 나는 잠시 동안 예를 플레이해야합니다

덕분에 볼 ... Example

3

기본적으로 Perl 스레드의 데이터는 전용입니다. 한 스레드의 변수를 업데이트해도 다른 스레드 (또는 주 스레드)의 해당 변수 값은 변경되지 않습니다. $val을 공유 변수로 선언하고자 할 것입니다. threads::shared을 참조하십시오.

나는 스크립트 하단에 $val을 공유로 표시 했으므로 너무 늦을 때까지 보지 못했습니다. 우연히도 Perl 인터프리터는 너무 늦을 때까지 선언문을 보지 않을 것입니다. 프로그램의 상위 95 %는 스크립트의 마지막에 선언 한 어휘, 공유 $var이 아닌 전역 스레드 전용 변수 $var을 조작합니다. 이 선언을 스크립트의 맨 위로 이동하십시오.

프로그램 상단에 use strict을 넣으면 시간이 아닌 몇 분의 슬픔이 생길 수 있습니다.

+0

'use strict'에 +1합니다. – snap

0

먼저 여기에 회신 죄송합니다,하지만 내 쿠키 또는 회신 등을 편집 할 수있는 기능을 잃었다 일을 끝내고 내가하고있는 일에 섞어 라.하지만 첫눈에 반듯이 ... 바르게 보인다. 고마워.

나는 사용하여 $ 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... 
} 

을하지만 다시 ... 감사를 당신의 방법을 시도 할 것이다.