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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.