I'm learning WxPerl as a replacement for some of my Tk apps and have a question. In several of my apps I currently have smaller Perl programs (turned into *.exe files via PerlApp) that I launch and comunicate with. I've decided to pull these smaller programs into my new WxPerl app in the form of sub routines. I need to have the ability to interact with the subs. I assume I need to use threads as I discovered in the sample I found below. How can I stop a thread on demand? Do I need to create some sort of listener or watchdog thread that kills the worker thread?

use threads; use threads::shared; use Thread::Queue; use strict; use Wx; 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], [300, 30 +0], wxTE_MULTILINE); $self->{button} = Wx::Button->new($self, -1, "&Get Sites"); $self->{button_stop} = Wx::Button->new($self, -1, "Stop"); my $sizer = Wx::BoxSizer->new(wxVERTICAL); $sizer->Add($self->{text}); $sizer->Add($self->{button}); $sizer->Add($self->{button_stop}); $self->SetSizer($sizer); $self->{text}->AppendText("test\n"); EVT_COMMAND($self, -1, $done_event, \&done); EVT_BUTTON($self,$self->{button}, \&on_button ); EVT_BUTTON($self,$self->{button_stop}, \&test ); my $sendqueue = Thread::Queue->new(); my $worker = threads->create(\&worker, $self, $sendqueue ); $self->{queue} = $sendqueue; $self->{worker} = $worker; $self->Fit; return $self; } sub test { my ($self, $event) = @_; $self->{queue}->enqueue('STOP'); } sub on_button { my ($self, $event) = @_; $self->{queue}->enqueue('GO'); } sub done { my ($self, $event) = @_; my $text = $event->GetData; $self->{text}->AppendText("$text\n"); } sub worker { my($handler, $queue) = @_; use LWP::Simple; while (my $message = $queue->dequeue ) { return 1 if $message eq 'STOP'; my @sites = qw( http://www.google.com/ http://www.microsoft.com/ http://www.yahoo.com/ http://www.cpan.org/ http://www.perl.org/ ); # For testing purposes for(0..10) { for(0 .. $#sites) { my $page = get($sites[$_]); my ($title1) = $page =~ /<title[^>]*>\s*(.+?)<\/title[^>]*>/gs +i; my $title : shared = $title1; print "$title\n"; my $thread_event = Wx::PlThreadEvent->new(-1, $done_event, $ti +tle); Wx::PostEvent($handler, $thread_event); } } } } package main; MyApp->new->MainLoop;

Thanks, -Paul

In reply to Wx subs and threads by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.