1Wx::Thread(3)         User Contributed Perl Documentation        Wx::Thread(3)
2
3
4

NAME

6       Thread - using wxPerl with threads
7

SYNOPSIS

9         # the order of these use()s is important
10         use threads;
11         use threads::shared;
12         use Wx;
13
14         my $DONE_EVENT : shared = Wx::NewEventType;
15
16         my $worker = threads->create( \&work );
17
18         # create frames, etc
19         my $frame = Wx::Frame->new( ... );
20         EVT_COMMAND( $frame, -1, $DONE_EVENT, \&done );
21         $app->MainLoop;
22
23         sub done {
24             my( $frame, $event ) = @_;
25
26             print $event->GetData;
27         }
28
29         sub work {
30             # ... do stuff, create a shared $result value
31
32             my $threvent = new Wx::PlThreadEvent( -1, $DONE_EVENT, $result );
33             Wx::PostEvent( $frame, $threvent );
34         }
35
36         # event handler
37         sub OnCreateThread {
38             # @_ = () is necessary to avoid "Scalars leaked"
39             my( $self, $event ) = @_; @_ = ();
40
41             threads->create( ... );
42         }
43

DESCRIPTION

45       Threaded GUI application are somewhat different from non-GUI threaded
46       applications in that the main thread (which runs the GUI) must never
47       block.  Also, in wxWidgets, no thread other than the main thread can
48       manipulate GUI objects.  This leads to a hybrid model where worker
49       threads must send events to the main thread in order to change the GUI
50       state or signal their termination.
51
52       Order of module loading
53
54       It's necessary for "use Wx" to happen after <use threads::shared>.
55
56       Sending events from worker threads
57
58       "Wx::PlThreadEvent" can be used to communicate between worker and GUI
59       threads.  The event can carry a shared value between threads.
60
61         my $DONE_EVENT : shared = Wx::NewEventType;
62
63         sub work {
64             # ... do some stuff
65             my $progress = new Wx::PlThreadEvent( -1, $DONE_EVENT, $progress );
66             Wx::PostEvent( $frame, $progress );
67
68             # ... do stuff, create a shared $result value
69             my $end = new Wx::PlThreadEvent( -1, $DONE_EVENT, $result );
70             Wx::PostEvent( $frame, $end );
71         }
72
73       The target of the event can be any "Wx::EvtHandler"
74
75       Receiving events from worker threads
76
77       "Wx::PlThreadEvent" is a command event and can be handled as such.  The
78       "->GetData" method can be used to retrieve the shared data contained
79       inside the event.
80
81         my $DONE_EVENT : shared = Wx::NewEventType;
82
83         EVT_COMMAND( $frame, -1, $DONE_EVENT, \&done );
84
85         sub done {
86             my( $frame, $event ) = @_;
87
88             print $event->GetData;
89         }
90
91       Creating new threads
92
93       Creating new threads from event handlers works without problems except
94       from a little snag.  In order not to trigger a bug in the Perl inter‐
95       preter, all event handler that directly or indirectly cause a thread
96       creation must clean @_ before starting the thread.
97
98       For example:
99
100         sub OnCreateThread {
101             my( $self, $event ) = @_; @_ = ();
102
103             threads->create( ... );
104         }
105
106       failure to do that will cause "scalars leaked" warnings from the Perl
107       interpreter.
108
109
110
111perl v5.8.8                       2007-03-10                     Wx::Thread(3)
Impressum