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__

Reply via email to