Here is the update code with the same error.
I would like to be able to move my Tk window when a button is press.
Currently, it's freeze until it's done parsing.
I get all sorts of errors regarding thread corruption, since you
created your thread after Tk is invoked, and you try to access the
Tk text widget from the thread. Gtk2 will allow you to do that, but Tk
won't. Even Gtk2 needs some precautions.
This is a basic Tk thread script, to show you the ropes.
You can search groups.google.com for "perl Tk threads"
and find all of the many examples that have been posted previously.
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use threads;
use threads::shared;
my $go:shared = 0;
my $die:shared = 0;
my $count:shared = 0;
# make thread before any Tk is called,
# usually a sleeping thread
my $thr = threads->new(\&new_thread);
my $mainWindow = MainWindow->new;
my $textBox = $mainWindow->Scrolled("Text", -width=>80, -height=>7,
-scrollbars=>"ose", -wrap=>"word")->pack();
$mainWindow->Button(-foreground=>"blue", -text=>"Click",
-command=>\&excecute)->pack();
$mainWindow->Button(-foreground=>"red", -text=>"Stop",
-command=>sub{ $go=0 })->pack();
$mainWindow->Button(-foreground=>"black", -text=>"exit",
-command=>sub{ $die=1;
$thr->join;
exit;
})->pack();
# you must actively read the shared variables with a timer
# filehandles can be shared thru the fileno,
# search groups.google for examples
my $repeater = $mainWindow->repeat(1000,sub{
if($go){
$textBox->insert('end', "$count\n");
$textBox->see('end');
$textBox->update;
}
});
MainLoop;
sub new_thread{
while(1){
if($die){return} #thread can only be joined after it returns
if( $go == 1){
if($die){return}
$count++;
}else{ select undef,undef,undef,.1} #sleep awhile
}
}
sub excecute { $go = 1 }
__END__
Those are the basics, but search groups.google.com, because
we have answered these questions many times before, with code examples.
zentara