use strict;
use threads;
use warnings;
use Win32::GUI qw();
use LWP::UserAgent;
my $window = Win32::GUI::DialogBox->new( -title => "Test", -name => "T
+est", -onTimer => sub { return poll() } );
my $use_up_some_memory = join( "", "x" x (1024 * 1024 * 20) );
$window->AddTimer( "T1", 20000 );
$window->Show;
Win32::GUI::Dialog();
sub poll
{
my $rss : shared;
print "polling\n";
my $thread = threads->create( sub {
my $ua = LWP::UserAgent->new;
my $response = $ua->get( 'http://www.mektek.net/mekmatch/listS
+erversRss.mkz' );
$rss = $response->decoded_content
if $response->is_success;
} );
while ( $thread->is_running ) {
sleep(1); Win32::GUI::DoEvents;
}
return unless $rss;
}
holli
You can lead your users to water, but alas, you cannot drown them.
| [reply] [d/l] |
I think you really need to do a $thread->join at the end of your subroutine.
But instead of polling, I would have the thread send a (Windows) message to the main thread to tell the main thread/window that it can now fetch the data and update things.
| [reply] [d/l] |
| [reply] |
As I suspected it (and Corion called it), you constantly start new threads and never join or detach them, then they just hang around consuming memory once they've completed.
Also, as the last line of the thread sub is $rss, that is returned from the thread, consuming more memory. The fact that you've shared it, as well as returning it means you've consumed even more.
And while I'm critiquing, there are some other anomalies in your code:
- "x" x (1024 * 1024 * 20) is a single string of 20MB 'x's.
The join is utterly redundant.
- return unless $rss;
Return (nothing) unless $rss has some value.
Otherwise, just fall off the end of the subroutine and return ???
Try something like this (untested):
#! perl -slw
use strict;
use threads;
use Win32::GUI qw();
use LWP::UserAgent;
sub t {
my $rss;
my $ua = LWP::UserAgent->new;
my $response = $ua->get(
'http://www.mektek.net/mekmatch/listServersRss.mkz'
);
$rss = $response->decoded_content
if $response->is_success;
print "done1";
return $rss;
}
my $window = Win32::GUI::DialogBox->new(
-title => "Test", -name => "Test", -onTimer => sub { return poll()
+ }
);
my $use_up_some_memory = join( "", "x" x (1024 * 1024 * 20) );
$window->AddTimer( "T1", 5000 );
$window->Show;
Win32::GUI::Dialog();
sub poll
{
print "polling ... ";
my $thread = threads->create( \&t );
while ( $thread->is_running ) {
Win32::Sleep 10;
Win32::GUI::DoEvents;
}
print " done2";
return $thread->join;
}
Presumably you want to do something with the data returned from poll?
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |