Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shared data between threads and perl/tK help

Status
Not open for further replies.

mastermagrath

Technical User
May 21, 2004
28
0
0
US
Hi,

Wonder can anyone give me some help.
I've written a few scripts with tk and threads and everything has been fairly straightforward until i hit this little problem. Basically i have a small GUI consisting of a Label widget. The text variable in this widget points to a shared variable. I start a new thread which periodically changes this shared variable. The problem is even though the variable is getting updated, the label widget doesn't update its text!? I know the label at least reads the shared variable on start up as it takes the initial value and i know that the variable is updated as i print out the value to STDOUT.
I've included a snippet of the program (assume all necessary modules used)

my $rate : shared = 0;
my $mw = MainWindow->new;
$mw->title('test');
my $f1 = $mw->Frame(-borderwidth => 2, -relief => 'groove')->pack;
my $f1LabelBytesPerSec = $f1->Label(-textvariable => \$rate, -width => 15)->pack;
my $f1ButtonStart = $f1->Button(-text => 'Start', -command => \&Start)->pack(-side => 'left');
MainLoop;

sub Start {
my $thr = threads->new(\&Update);
}

sub Update{
while(1) {
$rate += 1;
sleep 5;
}
}

I'd really appreciate any comments as it seems like such a basic thing programmers would do when writing GUI's and using threads.
 
Try passing the label into your Update() subroutine. Then you can update its value directly after the line in which you increment $rate each time.
 
Thanks ishnid,

I'm not really sure how to do that, i've played a bit but keep getting funny errors? To tell the truth i'm not too hot on pointers and objects etc. Could you give me an idea?

Cheers
 
I don't use Tk, and am currently rusty on threads too. This probably won't work but should give you an idea:
Code:
my $rate : shared = 0;
my $mw = MainWindow->new;
$mw->title('test');
my $f1 = $mw->Frame(-borderwidth => 2, -relief => 'groove')->pack;
my $f1LabelBytesPerSec = $f1->Label(-textvariable => \$rate, -width => 15)->pack;
my $f1ButtonStart = $f1->Button(-text => 'Start', -command => \&Start)->pack(-side => 'left');
MainLoop;

sub Start {
# pass the label to the `new' method, which will pass it
# into Update()
my $thr = threads->new(\&Update, $f1LabelBytesPerSec);
}

sub Update{
    my $label = shift; # get the label 
    while(1) {
    $rate += 1;
    $label->whatever_the_name_of_the_method_to_change_the_text_is($rate);
    sleep 5;
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top