Manual Pages for UNIX Darwin command on man Wx::Thread
MyWebUniversity

Manual Pages for UNIX Darwin command on man Wx::Thread

Wx::Thread(3) User Contributed Perl Documentation Wx::Thread(3)

NAME

Thread - using wxPerl with threads

SYNOPSIS

# the order of these use()s is important

use threads; use threads::shared; use Wx;

my $DONEEVENT : shared = Wx::NewEventType;

my $worker = threads->create( \&work );

# create frames, etc

my $frame = Wx::Frame->new( ... );

EVTCOMMAND( $frame, -1, $DONEEVENT, \&done );

$app->MainLoop;

sub done {

my( $frame, $event ) = @;

print $event->GetData;

} sub work {

# ... do stuff, create a shared $result value

my $threvent = new Wx::PlThreadEvent( -1, $DONEEVENT, $result );

Wx::PostEvent( $frame, $threvent );

}

# event handler

sub OnCreateThread {

# @ = () is necessary to avoid "Scalars leaked"

my( $self, $event ) = @; @ = ();

threads->create( ... );

}

DESCRIPTION

Threaded GUI application are somewhat different from non-GUI threaded

applications in that the main thread (which runs the GUI) must never block. Also, in wxWidgets, no thread other than the main thread can manipulate GUI objects. This leads to a hybrid model where worker threads must send events to the main thread in order to change the GUI state or signal their termination. OOrrddeerr ooff mmoodduullee llooaaddiinngg It's necessary for "use Wx" to happen after . SSeennddiinngg eevveennttss ffrroomm wwoorrkkeerr tthhrreeaaddss "Wx::PlThreadEvent" can be used to communicate between worker and GUI threads. The event can carry a shared value between threads.

my $DONEEVENT : shared = Wx::NewEventType;

sub work {

# ... do some stuff

my $progress = new Wx::PlThreadEvent( -1, $DONEEVENT, $progress );

Wx::PostEvent( $frame, $progress );

# ... do stuff, create a shared $result value

my $end = new Wx::PlThreadEvent( -1, $DONEEVENT, $result );

Wx::PostEvent( $frame, $end );

} The target of the event can be any "Wx::EvtHandler" RReecceeiivviinngg eevveennttss ffrroomm wwoorrkkeerr tthhrreeaaddss "Wx::PlThreadEvent" is a command event and can be handled as such. The

"->GetData" method can be used to retrieve the shared data contained

inside the event.

my $DONEEVENT : shared = Wx::NewEventType;

EVTCOMMAND( $frame, -1, $DONEEVENT, \&done );

sub done {

my( $frame, $event ) = @;

print $event->GetData;

} CCrreeaattiinngg nneeww tthhrreeaaddss Creating new threads from event handlers works without problems except from a little snag. In order not to trigger a bug in the Perl interpreter, all event handler that directly or indirectly cause a thread creation must clean @ before starting the thread. For example: sub OnCreateThread {

my( $self, $event ) = @; @ = ();

threads->create( ... );

} failure to do that will cause "scalars leaked" warnings from the Perl interpreter.

perl v5.8.8 2007-03-16 Wx::Thread(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™