Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

zentara was kind enough to answer my earlier question and now I have the issue fixed. My next question based on the example I found below is how can I stop a runing thread gracefully without killing the main app?

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->{stop_button} = Wx::Button->new($self, -1, "&Stop Thread"); my $sizer = Wx::BoxSizer->new(wxVERTICAL); $sizer->Add($self->{text}); $sizer->Add($self->{button}); $sizer->Add($self->{stop_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 ); EVT_BUTTON($self,$self->{stop_button}, \&off_button ); my $sendqueue = Thread::Queue->new(); my $worker = threads->create(\&worker, $self, $sendqueue ); $self->{queue} = $sendqueue; $self->{worker} = $worker; $self->Fit; return $self; } sub on_button { my ($self, $event) = @_; $self->{queue}->enqueue('GO'); } sub off_button { my ($self, $event) = @_; $self->{queue}->enqueue('STOP'); # $self->{worker}->join; # $event->Skip(1); print "Stopped\n"; } sub on_close { my ($self, $event) = @_; $self->{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($self, $event) = @_; use Data::Dumper; print Dumper($self); print Dumper($event); use LWP::Simple; while (my $message = $event->dequeue ) { print "IN HERE\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/ ); 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($self, $thread_event); } } } package main; MyApp->new->MainLoop;

My current solution isn't working.

Thanks again, -Paul

Replies are listed 'Best First'.
Re: stopping a thread in wxPerl?
by zentara (Cardinal) on Nov 23, 2009 at 20:46 UTC
    ... a thread must return for it to stop ( other than killing it)..... it seems you enqueue 'Stop', but maybe work on a better, more direct way?
    while (my $message = $event->dequeue ) { print "IN HERE\n"; return 1 if $message eq 'STOP';
    ... maybe $event->dequeue is hanging? ..... maybe experiment there with a counter and see how many times it prints STOP and figure out why it dosn't return ?

    ... maybe try a time delay to give the eventloop enough loop time to get it all done?

    $self->{queue}->enqueue('STOP'); # insert a small Wx time delay here, or use a conditional in the join +like: do 1 until $self->{worker}->join; #or code to wait for the thread count to change }
    in other words find the best Wx way to wait there for the thread to return

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: stopping a thread in wxPerl?
by Anonymous Monk on Nov 24, 2009 at 10:17 UTC