use threads; use threads::shared; use Wx; use Thread::Queue; use strict; # Maximum working threads my $MAX_THREADS = 1; # Flag to inform all threads that application is terminating my $TERM :shared = 0; # Prevents double detach attempts my $DETACHING :shared; package MyApp; use base 'Wx::App'; sub OnInit { my $self = shift; my $frame = MyFrame->new(); $frame->Show(1); $self->SetTopWindow($frame); return 1; } package MyFrame; use base 'Wx::Frame'; use Wx qw(wxTE_MULTILINE wxVERTICAL wxID_DEFAULT); use Wx::Event qw(EVT_COMMAND EVT_CLOSE EVT_BUTTON); my $done_event : shared = Wx::NewEventType; sub new { my $class = shift; my $self = $class->SUPER::new(undef, -1, "The title"); $self->{text} = Wx::TextCtrl->new($self, -1, "", [-1,-1], [180, 10 +0], wxTE_MULTILINE); $self->{button_start} = Wx::Button->new($self, -1, "&Start Test"); $self->{button_stop} = Wx::Button->new($self, -1, "&Stop Test"); my $sizer = Wx::BoxSizer->new(wxVERTICAL); $sizer->Add($self->{text}); $sizer->Add($self->{button_start}); $sizer->Add($self->{button_stop}); $self->SetSizer($sizer); $self->{text}->AppendText("test\n"); EVT_BUTTON($self,$self->{button_start}, \&DeployTestThread ); EVT_BUTTON($self,$self->{button_stop}, \&StopTestThread ); return $self; } sub DeployTestThread { # Manage the thread pool until signalled to terminate while (! $TERM) { # Keep max threads running for (my $needed = $MAX_THREADS - threads->list(); $needed && ! $TERM; $needed--) { # Create New thread my $thread = threads->create('ExecuteTest'); } } # Detach and kill any remaining threads foreach my $thr (threads->list()) { lock($DETACHING); $thr->detach() if ! $thr->is_detached(); print "Killing " . $thr->tid() . "\n"; $thr->kill('KILL'); } print("Done\n"); } sub StopTestThread { $TERM = 1; } sub ExecuteTest { my($handler, $queue) = @_; use LWP::Simple; # For Testing purposes for (1..10) { my @sites = qw( http://www.google.com/ http://www.microsoft.com/ http://www.yahoo.com/ http://www.cpan.org/ http://www.perl.org/ ); for(0 .. $#sites) { my $page = get($sites[$_]); my ($title1) = $page =~ /<title[^>]*>\s*(.+?)<\/title[^>]* +>/gsi; my $title : shared = $title1; print "$title\n"; my $thread_event = Wx::PlThreadEvent->new(-1, $done_event, + $title); Wx::PostEvent($handler, $thread_event); } } } package main; MyApp->new->MainLoop;
In reply to Re^3: Wx subs and threads
by Anonymous Monk
in thread Wx subs and threads
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |