Hi,
I have a GUI with one button. When the user is pressing on this button, numbers are printed to the console. They start with 0 and are then increasing (0,1,2,3,4,5,...). The function which is printing the numbers to the console is running in another thread, so that the user is still able to use the GUI.
When the user is pressing again on the button, the old thread shall be stopped. And a new thread shall be started printing numbers.
I tried to implement this with the following code.
#!/usr/bin/perl use strict; use warnings; use Tk; use threads; use threads::shared; my $mw = new MainWindow; my $button = $mw->Button('-relief' => 'raised', '-text' => 'CreateNumbers', '-command' => sub { &createNumberThread(0); }); $button->pack(); MainLoop(); BEGIN { my $nb_thread = undef; my $run :shared = 1; sub createNumberThread { my $nb = $_[0]; # delete currently active thread if one is available if( defined $nb_thread ) { $run = 0; } $nb_thread = threads->new(\&createNumbers, $nb); $nb_thread->detach(); $run = 1; sub createNumbers { my $nb = $_[0]; while($run) { print $nb . "\n"; $nb++; } } } }
But it does not seem to work well. Because when I press several times on the button, I get the following error message back.
Attempt to free non-existent shared string '_XEvent_', Perl interprete +r: 0x1ca69ac at c:/Perl/site/lib/Tk/Widget.pm line 98 during global d +estruction. Free to wrong pool 1ca6060 not 246f88 at c:/Perl/site/lib/Tk/Widget.pm + line 98 during global destruction.
Any hints and help is welcome.
Thank you
Dirk
In reply to Thread problems by Dirk80
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |