Janish has asked for the wisdom of the Perl Monks concerning the following question:

Hello greetings! I would like to trigger run for 2 buttons (or more) at the same time with another button call "RunAll". Each buttons (I call it button1 and button2) have it's own subroutine. I manage to trigger run for this new "RunAll" button to call the button1 and button2 up but it only run one after another, I would like the new "RunAll" button to trigger run in parallel as due to the long run time for one of the button. Can anyone please help me on this?

Below is my code

my $run_all=$mwb_final->Button( -text => 'RunAll', -width => 100, -activebackground => 'blue', -background => 'green', -command => sub { $first_button->invoke(); $second_button->invoke(); #push @jobs, threads->create(sub {$first_button->invoke();}); #push @jobs, threads->create(sub {$second_button->invoke();}); #$_->join for @jobs; # Wait for everything to finish. })->pack();

p/s:I tried with another option of coding as the one which commented out (using threds, and I did put "use threads" on top of my code), but it seems not works for me either. I get errors as below

> Thread 1 terminated abnormally: Can't call method "tagAdd" on an u +ndefined value at /usr/pkgs/perl/5.14.1-threads/lib64/module/default/ +x86_64-linux-thread-multi/Tk/Text.pm line 158. Thread 2 terminated abnormally: Can't call method "tagAdd" on an undef +ined value at /usr/pkgs/perl/5.14.1-threads/lib64/module/default/x86_ +64-linux-thread-multi/Tk/Text.pm line 158. Attempt to free non-existent shared string '_ErrorInfo_', Perl interpr +eter: 0x602010 at /usr/pkgs/perl/5.14.1-threads/lib64/module/default/ +x86_64-linux-thread-multi/Tk.pm line 424.

Replies are listed 'Best First'.
Re: How to run 2 buttons in parallel in Perl/Tk
by zentara (Cardinal) on Jun 30, 2014 at 09:36 UTC
    You are creating threads after you invoked some Tk gui code. Tk is not threadsafe, and you cannot make threads that way. See Re: Perl Tk and Threads or you can switch to Gtk2 which allows your type of code, with some restrictions.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Hi Zentara, thanks for your answer, again. May I know how do people usually do if they want to kick run numerous job together? Seems that the treads method not suitable here, and I doubt to switch to Gtk2 now since my code now is pretty done except for this "RunAll" button :(

        You can use threads made before any gui code is called, and send commands to be eval'd by them thru threads:shared variables. See Tk-with-worker-threads

        The alternative, is to fork off your parallel processes, and that is the way it was originally done before the coming of perl threads.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh