24
24
if ( condition_for_not_running_the_task ) {
31
31
# This is run in a worker thread and may take a long-ish
32
32
# time to finish. It must not touch the GUI, except through
33
# Wx events. TODO: explain how this works
33
# Wx events. TO DO: explain how this works
36
36
# Do something that takes a long time!
66
66
to GUI objects and similar things to the finish event handler
67
67
since these must not be accessed from worker threads.
69
However, you should be cautious when keeping references to GUI
70
elements in your tasks, in case the GUI wants to destroy them
71
before your task returns.
73
Instead, it is better if your C<finish> method knows how to
74
relocate the GUI element from scratch (and can safely handle
75
the situation when the GUI element is gone, or has changed enough
76
to make the task response irrelevent).
71
This is the base class of all background operations in Padre. The SYNOPSIS
72
explains the basic usage, but in a nutshell, you create a subclass, implement
73
your own custom C<run> method, create a new instance, and call C<schedule>
74
on it to run it in a worker thread. When the scheduler has a free worker
75
thread for your task, the following steps happen:
80
This is the base class of all background operations in Padre.
81
The SYNOPSIS explains the basic usage, but in a nutshell, you create a
82
subclass, implement your own custom C<run> method, create a new instance,
83
and call C<schedule> on it to run it in a worker thread. When the scheduler
84
has a free worker thread for your task, the following steps happen:
113
122
use Scalar::Util ();
114
123
use Params::Util '_INSTANCE';
116
our $VERSION = '0.50';
118
# TODO: Why are there require?
125
our $VERSION = '0.60';
127
# set up the stdout/stderr printing events => initialized during run time
128
our $STDOUT_EVENT : shared;
129
our $STDERR_EVENT : shared;
131
# TO DO: Why are these require?
120
133
require Padre::Wx;
141
149
inherit. It simply stores all provided data in the internal
156
my $self = bless {@_} => $class;
157
if ( not defined $STDOUT_EVENT ) {
158
$STDOUT_EVENT = Wx::NewEventType();
159
$STDERR_EVENT = Wx::NewEventType();
146
166
C<Padre::Task> implements the scheduling logic for your
155
my $events_initialized = 0;
175
my $event_hooks_initialized = 0;
158
178
my $self = shift;
159
if ( !$events_initialized ) {
160
my $main = Padre->ide->wx->main;
179
if ( not $event_hooks_initialized ) {
180
$event_hooks_initialized = 1;
181
my $main = Padre->ide->wx;
161
182
Wx::Event::EVT_COMMAND(
183
This is the method that'll be called in the worker thread.
203
This is the method that will be called in the worker thread.
184
204
You must implement this in your subclass.
186
206
You must not interact with the Wx GUI directly from the
187
207
worker thread. You may use Wx thread events only.
188
TODO: Experiment with this and document it.
208
TO DO: Experiment with this and document it.
304
322
my $userclass = $padretask->{_process_class};
305
323
delete $padretask->{_process_class};
307
no strict 'refs'; ## no critic
308
326
my $ref = \%{"${userclass}::"};
309
327
use strict 'refs';
310
328
my $loaded = exists $ref->{"ISA"};
311
unless ( $loaded or eval("require $userclass;") ) { ## no critic
329
unless ( $loaded or eval("require $userclass;") ) {
314
332
Carp::croak("Failed to load Padre::Task subclass '$userclass': $@");
415
433
# The main-thread stdout hook
417
my ( $main, $event ) = @_;
435
my ( $wx, $event ) = @_;
418
436
@_ = (); # hack to avoid "Scalars leaked"
419
my $out = $main->output();
437
my $main = $wx->main;
438
my $out = $main->output();
420
439
$main->show_output(1);
421
440
$out->style_neutral();
422
441
$out->AppendText( $event->GetData );
426
445
# The main-thread stderr hook
428
my ( $main, $event ) = @_;
447
my ( $wx, $event ) = @_;
429
448
@_ = (); # hack to avoid "Scalars leaked"
430
my $out = $main->output();
449
my $main = $wx->main;
450
my $out = $main->output();
431
451
$main->show_output(1);
432
452
$out->style_bad();
433
453
$out->AppendText( $event->GetData );
442
462
$task->task_print("Hi this is immediately sent to the Padre output window\n");
444
Sends an event to the main Padre thread and dispays a
464
Sends an event to the main Padre thread and displays a
445
465
message in the Padre output window.
459
479
$task->task_warn("Hi this is immediately sent to the Padre output window\n");
461
Sends an event to the main Padre thread and dispays a
481
Sends an event to the main Padre thread and displays a
462
482
message in the Padre output window with style C<bad>.
483
503
You can set up a new event ID in your Padre::Task subclass
486
our $FUN_EVENT_TYPE = : shared = Wx::NewEventType();
506
our $FUN_EVENT_TYPE : shared;
507
BEGIN { $FUN_EVENT_TYPE = Wx::NewEventType(); }
488
509
Then you have to setup the event handler (for example in the
489
510
C<prepare()> method. This should happen in the main thread!
498
519
\&update_gui_with_fun
501
522
sub update_gui_with_fun {
502
523
my ($main, $event) = @_; @_=(); # hack to avoid "Scalars leaked"
503
524
my $data = $event->GetData();
506
527
After that, you can dispatch events of type C<$FUN_EVENT_TYPE>
507
528
by simply running:
520
541
unless ( defined $data and length($data) );
523
$Padre::TaskManager::_main,
524
545
Wx::PlThreadEvent->new( -1, $eventid, $data ),
535
556
Since the task objects are transferred to the worker threads via
536
557
C<Storable::freeze()> / C<Storable::thaw()>, you cannot put any data
537
558
into the objects that cannot be serialized by C<Storable>. I<To the best
538
of my knowledge>, that includes filehandles and code references.
559
of my knowledge>, that includes file handles and code references.
552
573
=head1 COPYRIGHT AND LICENSE
554
Copyright 2008-2009 The Padre development team as listed in Padre.pm.
575
Copyright 2008-2010 The Padre development team as listed in Padre.pm.
556
577
This program is free software; you can redistribute it and/or
557
578
modify it under the same terms as Perl 5 itself.
561
# Copyright 2008-2009 The Padre development team as listed in Padre.pm.
582
# Copyright 2008-2010 The Padre development team as listed in Padre.pm.
563
584
# This program is free software; you can redistribute it and/or
564
585
# modify it under the same terms as Perl 5 itself.