I always struggle with threads in WxPerl. Using this WxPerl example I found on nabble, I'm trying to introduce a way to stop a running thread. It always seems to finish the thread and I can't stop it. The code is below:
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, wxID_DEFAULT, "Get Sites" +); my $sizer = Wx::BoxSizer->new(wxVERTICAL); $sizer->Add($self->{text}); $sizer->Add($self->{button}); $self->SetSizer($sizer); $self->{text}->AppendText("test\n"); EVT_COMMAND($self, -1, $done_event, \&done); EVT_CLOSE($self, \&on_close); EVT_BUTTON($self,$self->{button}, \&on_button ); my $sendqueue = Thread::Queue->new(); my $worker = threads->create(\&worker, $self, $sendqueue ); $self->{get_queue} = $sendqueue; $self->{worker} = $worker; $self->Fit; return $self; } sub on_button { my ($self, $event) = @_; if ( $self->{button}->GetLabel() eq 'Get Sites') { $self->{get_queue}->enqueue('GO'); $self->{button}->SetLabel('Stop'); } elsif ($self->{button}->GetLabel() eq 'Stop') { $self->{get_queue}->enqueue('STOP'); # Trying to interupt and stop the current running worker thread worker($self, $self->{get_queue}); } } sub on_close { my ($self, $event) = @_; $self->{get_queue}->enqueue('STOP'); $self->{worker}->join; $event->Skip(1); } 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 ) { print "Message: $message\n"; 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/ http://www.perlmonks.com/ ); 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;
Any idea what I'm missing.

In reply to How to kill a running thread 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.