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

Esteemed Monks,

I have a Perl/Tk app running under Windows. I ask you assistance in two instances:

1.. I need to start an external program - leave it running and return to the original program. exec reaplaces the original, and system runs the program but the original is blocked waiting for a return. How can I 'unblock' the original?

2.. In a Tk application I want to pace the rate at which an operation is done. The programme is sending emails to advise of various actions taken by the programme, but I only want one to be sent every 'n' seconds. That is all this program does, but in the intervening period I would like to be able to move the window and/or gain focus. How can I do this?

jdtoronto

  • Comment on Run another program without blocking or exiting (Perl/Tk).

Replies are listed 'Best First'.
Re: Run another program without blocking or exiting (Perl/Tk).
by BrowserUk (Patriarch) on Sep 02, 2005 at 06:46 UTC
  • On windows,  system 1, $cmd; will run the command asynchronously.
  • The Tk MainWindow method repeat calls the sub you bind to it at the regular intervals you specify:
    my $mw = MainWindow->new; .... $mw->repeat( 100, sub{ ## This sub is called every 1/10 th of a second } );

    I'd give a pointer to the documentation for repeat, but I have never found it. I saw it in a program once and just use it.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      IPC::Open2 is a core module what will run a program in the background, and it's portable. (It uses system 1, in Windows, and fork+exec in unix.) I believe IPC::Run will do the same and has a better API, but it's not a core module.

        All true, but why are you telling me this?

      • The OP explicitly asked for a Windows solution.
      • IPC::Open2/Open3 are awkward and unreliable (on windows at least).
      • IPC::Run is a heavy, complicated behemoth.

        Any of the IPC::* modules is worth the effort of using, if you need the facilities they provide--ie. IPC between the spawned process and the spawning process. This requirement is not mentioned in the OP.

        Either way, wouldn't your post be better directed at OP (who may not know of these modules), rather than me (who does)?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      For future reference, (Tk)repeat is documented under (Tk)after.

Re: Run another program without blocking or exiting (Perl/Tk).
by reneeb (Chaplain) on Sep 02, 2005 at 11:00 UTC
    You can also use Proc::Background:
    use Proc::Background; my $proc1 = Proc::Background->new("explorer http://perlmonks.org"); print "continue without waiting for the process\n";
Re: Run another program without blocking or exiting (Perl/Tk).
by polypompholyx (Chaplain) on Sep 02, 2005 at 11:13 UTC

    In answer to the first question, you could use cmd's start utility...

    use strict; use Tk; my $mw = MainWindow->new(); $mw->Button( -text => 'Start Word', -command => sub { system 'start WINWORD.EXE' }, )->pack; MainLoop();

Re: Run another program without blocking or exiting (Perl/Tk).
by zentara (Cardinal) on Sep 02, 2005 at 11:33 UTC
    Why not run it in a thread? From what I gather, windows is thread based, so it should work. Create your Tk in your main thread and create a worker thread to run your command in. An example is at Tk-with-worker-threads

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