in reply to Re: how to fork & join in tk?
in thread how to fork & join in tk?

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