If running on a Unix platform, you're in luck. One can spawn a Hobo process while Tk is running.

For this demonstration, it's best to disable the button prior to spawning to prevent running two or more Hobos. The text is updated just like before with "Step One", "Step Two", etc. Finally, the button state is set back to normal after the Hobo completes processing.

The hobo variable is either defined or not defined. Thus, state-like in itself.

use strict; use warnings; use Tk; use MCE::Hobo; use MCE::Shared; my $msg = MCE::Shared->scalar("Step One"); my $hobo; my $mw = MainWindow->new(); $mw->protocol( WM_DELETE_WINDOW => \&quit ); $mw->geometry("+150+100"); $mw->Label( -text => "Tk + MCE::Hobo Demo", -height => 2, -width => 22 + )->pack; my $btn1 = $mw->Button( -text => "Start", -command => \&fun, -width => + 10 ); $btn1->pack; my $btn2 = $mw->Button( -text => "Quit", -command => \&quit, -width => + 10 ); $btn2->pack; my $timer = $mw->repeat( 100, sub { return unless $hobo; my $text = $msg->get; $btn1->configure( -text => $text ); if ( $hobo->is_joinable ) { $hobo->join; if ( my $err = $hobo->error ) { print {*STDERR} "something went wrong: $err\n"; } $btn1->configure( -state => "normal" ); $hobo = undef; } }); MainLoop(); sub fun { # important, disable button and reset the shared variable $btn1->configure( -state => "disabled" ); $msg->set("Step One"); $hobo = MCE::Hobo->create( sub { sleep 1; $msg->set("Step Two"); sleep 1; $msg->set("Step Three"); }); return; } sub quit { $timer->cancel; $hobo->exit->join if $hobo; exit; }

Q. Why does this work using MCE::Hobo?

A. Hobos exit by calling CORE::kill("KILL", $$) when Tk is present to bypass any destructors during exiting.

Regards, Mario


In reply to Re^2: how to fork & join in tk? by marioroy
in thread how to fork & join in tk? by redss

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.