# Assuming my $mw = MainWindow(...)
sub button_press {
# Do some work
$mw->update;
# Do some more work
$mw->update;
# etc...
####
#!/usr/bin/perl -w
#
# Libraries
use strict;
use warnings;
use threads;
use Tk;
use Tk::Font;
use Tk::ROText;
# Main Program
my $mw = new MainWindow(-title => 'Tk thread example');
my $fnt = $mw->Font(-family => 'tahoma', -size => '24');
my $btn = $mw->Button(-text => 'Press Me', -font => $fnt)->pack(-fill => 'x');
$btn->configure(-bg => '#ff7f3f', -command => \&button_press);
my $txt = $mw->ROText(-bg => 'white')->pack(-fill => 'both');
$mw->bind('' => sub { exit });
$mw->repeat(1000 => \&gui_loop);
MainLoop;
# Subroutines
sub gui_loop {
$txt->insert('end', sprintf "Time is %s\n", scalar localtime time());
$txt->see('end');
}
sub button_press {
my $sthread = threads->new(sub { worker_thread() });
$sthread->detach();
# Here we "return immediately" :)
return;
}
sub worker_thread {
my $tid = threads->tid;
# But DON'T do this ... !!!
# my $thread_mw = new MainWindow();
for (my $i = 0; $i < 1024; $i++) {
printf "Thread %s: Doing more work ...\n", $tid;
sleep 1;
}
}
##
##
Thread 1: Doing more work ...
Attempt to free non-existent shared string '_ErrorInfo_', Perl interpreter: 0x23
3f3c at C:/Perl/site/lib/Tk.pm line 423.