athanasia has asked for the wisdom of the Perl Monks concerning the following question:
my $thread_die1 : shared; $thread_die1 = 0; my $thread_go1 : shared; $thread_go1 = 0; my $firstvar : shared; $firstvar = ''; my $thread1 = threads->new(\&getfirst); my $thread_die2 : shared; $thread_die2 = 0; my $thread_go2 : shared; $thread_go2 = 0; my $secondvar : shared; $secondvar = ''; my $thread2 = threads->new(\&getsecond); use Tk; use Tk::TopLevel; use utf8; use Win32; use LWP::Simple; use XML::Simple; use POSIX(":sys_wait_h"); $customfont = "verdana 8"; $mw = new MainWindow (-title => "My application"); $window_pos = "800x600+200+200"; $mw->geometry($window_pos); $mw->resizable(0,0); my $label = $mw->Label(-text => "Some text", -font=>$customfont)->plac +e(-x => 200, '-y' => 170); my $nextbutton = $mw -> Button(-text => "Next", -font => $customfont, -height => "1", -width => "15", -command =>\&allcompsscreen) -> place(-x => 520, '-y' => 420); MainLoop; sub allcompsscreen { my ($firsttxt, $secondtxt); $mw->withdraw(); $allcomps = $mw -> Toplevel(-title => "HTTP requests"); $allcomps->geometry($window_pos); $allcomps->resizable(0,0); $allcomps->attributes(-topmost=>1); my $waitmsg1 = $allcomps->Label(-text => "Waiting for first HT +TP GET", -font=>$customfont)->place(-x => 200, '-y' => 170); $thread_go1 = 1; my $timeout = 30; $allcomps->update(); while ((!defined $firstvar or $firstvar eq '') and $timeout > 0) { sleep 1; $allcomps->update(); $timeout -=1; } $thread_go1 = 0; $waitmsg1->placeForget; if ($firstvar eq 'A') { $firsttxt = $allcomps->Label(-text => "A", -font=>$custo +mfont)->place(-x => 200, '-y' => 170); } else { $firsttxt = $allcomps->Label(-text => "NOT A", -font=>$c +ustomfont)->place(-x => 200, '-y' => 170); } $allcomps->update; sleep 10; my $waitmsg2 = $allcomps->Label(-text => "Waiting for second +HTTP GET", -font=>$customfont)->place(-x => 200, '-y' => 270); $thread_go2 = 1; $timeout = 30; $allcomps->update(); while ((!defined $secondvar or $secondvar eq '') and $timeout > 0) + { sleep 1; $allcomps->update(); $timeout -=1; } $thread_go2 = 0; $waitmsg2->placeForget; if ($secondvar eq 'B') { $secondtxt = $allcomps->Label(-text => "B", -font=>$cust +omfont)->place(-x => 200, '-y' => 270); } else { my $secondtxt = $allcomps->Label(-text => "NOT B", -font +=>$customfont)->place(-x => 200, '-y' => 270); } $thread_die1 = 1; $thread_die2 = 1; } sub getfirst { my $url = "http://192.168.1.254/..."; while(1) { if ($thread_go1 == 1) { $firstvar = &firsthttpget($url); } else { sleep 1; } if ($thread_die1 == 1) { return; } } } sub getsecond { my $url = "http://192.168.1.254/..."; while(1) { if ($thread_go2 == 1) { $secondvar = &secondhttpget($url); } else { sleep 1; } if ($thread_die2 == 1) { return; } } } sub firsthttpget() { my $res; my $address = $_[0]; my $xmlobj = new XML::Simple; $address =~s/http:\/\//http:\/\/admin:admin\@/g; my $httpcontent = get $address; if (!defined $httpcontent or $httpcontent eq '') { $res = -1; } else { $res= $xmlobj->XMLin($httpcontent); } return $res; } sub secondhttpget { my $res; my $address = $_[0]; my $xmlobj = new XML::Simple; $address =~s/http:\/\//http:\/\/admin:admin\@/g; my $httpcontent = get $address; #Here the program hangs indefinite +ly if (!defined $httpcontent or $httpcontent eq '') { $res = -1; } else { $res = $xmlobj->XMLin($httpcontent); } return $res; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Threads and LWP get issue in Windows
by Anonymous Monk on Sep 04, 2009 at 15:14 UTC |