A trick to avoid this is giving back control to the Tk main loop. This can either be done by splitting up the "mainprocess" into singular tasks which call each other e.g. via Tk->after, or storing that main-processes state somehow, thus making it re-entrant. You could also use the POE framework to do either of those.

Term::ReadLine::Gnu uses another trick, it calls the Tk event handler DoOneEvent() directly - with due care, because it blocks if there's no event dispatched. If the above techniques (or just the use of POE) aren't feasible, you could use that trick.

See Tk::Event.

update:

The proffered solution... Clock that goes backwards enhanced with a CPU-intensive, long running task. While the task is as silly as the clock itself, it is suitable for the POC.

It's just a matter of adding a signal handler and calling the event dispatcher in a non-blocking way.

#!/usr/bin/perl use strict; use Time::HiRes qw(ualarm); use Tk; BEGIN {*Tk::Clock::time = sub { time * -1 } } use Tk::Clock; my $wm = MainWindow->new; my $ck = $wm->Clock->pack(-side => 'top'); my $b = $wm->Button(-text => 'Start Process', -command => \&mainproce +ss)->pack; $ck->config(dateFormat => "dd-mm-yyy"); MainLoop; # # simulate a long running, CPU-intensive task # sub mainprocess { local $SIG{ALRM} = sub { DoOneEvent(Tk::Event::DONT_WAIT); ualarm(1000); } ualarm(1000); # task body begin my $c; $b->configure(-text => 'running...'); while (1) { $c++; print $c,"\n" unless $c % 1000; last if $c == 10000000; } $b->configure(-text => 'Start Process'); # task body end ualarm(0); }

Pretty few lines of code added... well, I could shoehorn the alarm handler into one line, and it would be 3 lines - not counting the 'use Time::HiRes' line ;-)

I guess it's not necessary to offer a code conversion job for something that simple.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: Do I need threading for my GUI? by shmem
in thread Do I need threading for my GUI? by tamaguchi

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.