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!

cannot process more statements after thread is started 2

Status
Not open for further replies.

mastermagrath

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

Thanks for all your help and pointers, however i'm stuck again!! Can someone please please look at the small script i've written and point out why it wont work?! Basically i create a thread which prints hashes at $period seconds interval. $period is a shared variable. I expect the thread to go off and do its stuff. Then i create a slider widget that allows the user to change the value of $period.
The slider by itself works perfectly, the thread by itself works perfectly. However when i put them together, once the thread is created the hash printing loop seems to tie up the script and the widget is never created. This confuses me as i thought the whole point in threads was to allow parallel processing?

#C:\perl\bin\perl.exe -w

use Tk;
use strict;
use FileHandle;
use warnings;
use threads;
use threads::shared;

my $period : shared = 1;

my $thr = threads->new(\&hashes)->join;
$thr->detach;

sub hashes {
while () {
print "#";
flush STDOUT;
sleep $period;
}
}

my $scale_value = '1';
my $message = "Your period is $scale_value";

my $mw = MainWindow->new;
$mw->title('Scale');


my $label = $mw->Label(-textvariable => \$message);
my $age = $mw->Scale(-label => 'Age',
-variable => \$scale_value,
-orient => 'horizontal',
-from => 1,
-to => 9,
-resolution => 1,
-tickinterval => 20,
-command => \&display);
my $exit = $mw->Button(-text => 'Exit',
-command => [$mw => 'destroy']);
$label->pack;
$age->pack;
$exit->pack;

MainLoop;

sub display {
$message = "Your period is $scale_value";
$period = $scale_value;


}
 
Try this then:
Code:
#!/usr/bin/perl -w

use Tk;
use strict;
use FileHandle;
use threads;
use threads::shared;

my $period : shared = 1;

my $thr = threads->new(\&hashes);
#my $thr = threads->new(\&hashes)->join;
#$thr->detach;

sub hashes {
        while () {
                print "#";
                flush STDOUT;
                sleep $period;
        }
}

my $scale_value = '1';
my $message = "Your period is $scale_value";

my $mw = MainWindow->new;
$mw->title('Scale');


my $label = $mw->Label(-textvariable => \$message);
my $age = $mw->Scale(-label => 'Age',
                     -variable => \$scale_value,
                     -orient => 'horizontal',
                     -from => 1,
                     -to => 9,
                     -resolution => 1,
                     -tickinterval => 20,
                     -command => \&display);
my $exit = $mw->Button(-text => 'Exit',
                      -command => [$mw => 'destroy']);
$label->pack;
$age->pack;
$exit->pack;

MainLoop;

sub display {
  $message = "Your period is $scale_value";
  $period = $scale_value;
}
I changed your shebang line cos I'm running linux but you can use your own windoze shebang line.
Let me know if it works in windoze.


Trojan.
 
Trojan,

Man, i cant thank you enough, it works!!
I'll research myself why your change works. Its a little bit slow to pick up the change but again i'll look into this. Thanks again for all your help over the last week.......
 
That's ok.
Glad you're happy. :)
The "little bit slow to pick up the change" is probably changing down to a faster time.
You need to remember that if the delay is set to 10 seconds and you set it to one, that wont happen immediately. It can take up to 10 seconds for the code to finish the sleep before it detects the new delay value.


Trojan.
 
The call to join() was your problem - it waits for a thread to exit.

If speeding up is problematic, note that, under *ix, you can interrupt a sleep call (even in another process) by sending any signal. Does this work under 'doze?

f

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
You could try that but it may cause as many problems as it solves.

If the user keeps moving the slider up and down you would trigger a restart repeatedly and you could have a whole bunch of values sent when only one was intended.


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top