Hi Mark,
Thank you for the sample app.
I think it could be helpful if you could put it somewhere for others to
access it also, because there is so little WxPerl documentation anyway.
Octavian
----- Original Message -----
From: "Mark Dootson" <[EMAIL PROTECTED]>
To: "Octavian Rasnita" <[EMAIL PROTECTED]>
Cc: <[email protected]>
Sent: Sunday, April 20, 2008 5:00 PM
Subject: Re: WxPerl threads
Hi Octavian,
The OnCreateThread example is really just demonstrating a special case
when you start a thread inside an event handler. It doesn't concern your
example.
Your example does not work because it is posting events to '$self' where
'$self' is undefined.
I have made some amendments and return a 'working' version of your
example. Of course, you might not really want to start a thread inside
your frame constructor. It was just a quick way of getting working code
still using your example as a base.
If you have not done so, you should read perlthrtut in the perl docs. Its
a long read but explains just about everything I found.
Best of Luck
Mark
Octavian Rasnita wrote:
Hi,
I've read
http://search.cpan.org/~mbarbon/Wx-0.82/lib/Wx/Thread.pod
and I've started to create a test program (that I enclosed below), but
as the POD docs from the URL above doesn't include a working example, I
don't understand how to make it really work.
I've seen in the POD:
sub done {
my( $frame, $event ) = @_;
Isn't this subroutine the event handler? Or the following one?
sub OnCreateThread {
my( $self, $event ) = @_; @_ = ();
But if this is the event handler, where is it called in the program?
Then in this subroutine I've seen:
threads->create( ... );
Why should I create the threads in this subroutine when at the beginning
of the script I've done:
my $worker = threads->create( \&work );
Here below is the script I tried to make, although the worker thread
doesn't print anything in the TextCtrl.
Thank you.
Octavian
use threads;
use threads::shared;
use Wx;
my $done_event : shared = Wx::NewEventType;
my $worker = threads->create(\&worker);
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);
use Wx::Event qw(EVT_COMMAND);
sub new {
my $class = shift;
my $self = $class->SUPER::new(undef, -1, "The title");
$self->{text} = Wx::TextCtrl->new($self, -1, "", [100,100], [500, 500],
wxTE_MULTILINE);
$self->{text}->AppendText("test\n");
EVT_COMMAND($self, -1, $done_event, \&done);
return $self;
}
sub done {
my ($self, $event) = @_;
my $text = $event->GetData;
$self->{text}->AppendText("$text\n");
}
sub OnCreateThread {
my ($self, $event) = @_; @_ = ();
print "OnCreateThread\n";
}
package main;
MyApp->new->MainLoop;
sub worker {
use LWP::Simple;
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);
}
}
__END__