http://qs321.pair.com?node_id=938203

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

I read in a few places that to make the Perl Tk GUI responsive when a system() call is made to open a file, that it required threads.

system("documentation.doc");

Code above will open Microsoft Word and the documentation document. While that happens, the Perl Tk GUI is unresponsive until I shut Microsoft Word/documentation.doc down

So I am just making sure that threads are the best way to deal with this situation. If there is an easier, better, or different way that you recommend to tackle this problem, please point me in that direction

May your wisdom guide me

Replies are listed 'Best First'.
Re: Unresponsive Perl Tk GUI when using system() calls
by Marshall (Canon) on Nov 15, 2011 at 16:07 UTC
    If what you want to do is to launch word as a completely separate process and keep going, then use the Windows "START" command. Type help start at the command line for details.

    Perhaps you could explain what (if any) interaction your Tk application plans to have with this Word document process once it is started?

      The program creates a catalog index for my company. The word document was just an example (probably not the best, because it is only used for documentation). I let the user create and view text files and excel files (with WriteExcel) that will allow them to confirm the output and take the output and put it in the needed format for the catalog.

      Some of the processing in creating the index and the other tools/features I added to the program can take quite a few minutes to process. Many of my users have no clue about programming or computers (thus the GUI interface and usage of Cava Packager for executables), so they will want to open the excel file or text file to check them over and at the same time they expect to go back and use other features as that file is opened (from the Perl Tk interface) since the other tools/features take a long time to run.

      I want to be able to let them review the output and also be able to still operate the Perl Tk GUI at the same time.

        It sounds like Marshall was correct and you want to use the windows start command to start up the external program in a new process. Assuming you have the extension associated, you can just do:

        system("start documentation.doc")

        to start up the default application. If not, or if you want to use a non-default application, supply the executable name (if it is in the path) or the path to the executable (if it isn't).

        system("start wordpad.exe documentation.doc")
Re: Unresponsive Perl Tk GUI when using system() calls
by zentara (Archbishop) on Nov 15, 2011 at 16:30 UTC
    I read in a few places that to make the Perl Tk GUI responsive when a system() call is made to open a file, that it required threads.

    You are like the Sourcerer's Apprentice, :-) Threads by themselves don't magically make system calls non-blocking. You need to read deeper, and see that the system call needs to be run in a thread. This is called "blocking the Tk eventloop". See the Tk Event Loop

    You might run your non-blocking code like this simple thread example. See, your system call is in the thread.

    That is about as simple as I can explain it, of course, real code is usually a bit more complicated.

    #!/usr/bin/perl use warnings; use strict; use threads; my $thr = threads->new(\&sub1); print "thread running now\n"; $thr->join(); # wait for trivial thread to finish # and return sub sub1{ system("documentation.doc"); print "Done in thread\n"; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Unresponsive Perl Tk GUI when using system() calls
by salva (Canon) on Nov 15, 2011 at 16:53 UTC
Re: Unresponsive Perl Tk GUI when using system() calls
by Anonymous Monk on Nov 16, 2011 at 00:03 UTC