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

Fellow Monks:

I am starting a non-Perl process using Win32::Process and then trying to wait infinitely for the child to finish. In the meantime, the Perl parent process is checking the Windows clipboard to provide status messages to the user (this is a Tk app). (Therefore I can't use "system" which either waits or doesn't wait.)

If the parent sees that the clipboard is empty, it should fall thru to the $ProcessObj->Wait(INFINITE) statement, which it does, but that statement seems to have no effect. My Perl parent keeps processing and the child does too.

The same problem occurs if I comment out the entire clipboard stuff, so it appears to be strictly a problem with Win32::Process.

Here's the code:
use Tk; use Win32::Clipboard; use Win32::Process; $::CLIP = Win32::Clipboard(); # lots of code here Win32::Process::Create($ProcessObj, "d:\\mwa\\perlpps\\mwebprep.exe", "mwebprep $step", 0, NORMAL_PRIORITY_CLASS, ".") || die Win32ProcessError(); my $emptytries = 0; my $clip; while (1) { $clip = Win32::Clipboard::GetText(); if ($clip) { $::progresstext->insert('end', $clip . "\n"); $::progresstext->yview('moveto', 100); $::mainwin->update; $::CLIP->Empty(); $emptytries = 0; sleep(5); } else { $emptytries++; last if ($emptytries >= 3); sleep(1); } } $ProcessObj->Wait(INFINITE); # more code
Many thanks in advance for any advice!

Stephen

Replies are listed 'Best First'.
Re: Win32::Process not waiting
by GrandFather (Saint) on Feb 15, 2007 at 19:40 UTC

    It's often helpful if you can give a runnable sample of you code that exhibits the problem. The following is based on your sample and runs as expected on my system:

    use strict; use warnings; use Tk; use Win32::Clipboard; use Win32::Process; $::CLIP = Win32::Clipboard(); my $ProcessObj; Win32::Process::Create($ProcessObj, 'c:\Windows\system32\notepad.exe', "notepad", 0, NORMAL_PRIORITY_CLASS, ".") || die "Can't create"; my $emptytries = 0; my $clip; while (1) { $clip = Win32::Clipboard::GetText(); if ($clip) { print "$clip\n\n"; $::CLIP->Empty(); $emptytries = 0; sleep(5); } else { $emptytries++; last if ($emptytries >= 3); print "."; sleep(1); } } $ProcessObj->Wait(INFINITE); print "Done";

    Prints:

    asddf ..Done

    DWIM is Perl's answer to Gödel
      Sorry, will do so in future. It's much appreciated!

        You may like to run the sample I provided and verify that it behaves as expected on your system. If it does, alter it until it doesn't then post the result and tell us how it fails your expectations. If it does, tell us how it fails your expectations and show us the versions of modules and Perl that you are using. The less we have to guess the more we can help with your actual problem.


        DWIM is Perl's answer to Gödel
Re: Win32::Process not waiting
by ikegami (Patriarch) on Feb 15, 2007 at 18:18 UTC
    use strict; use warnings; use Win32::Process qw( INFINITE ); { Win32::Process::Create( my $ProcessObj, "$ENV{SystemRoot}\\System32\\notepad.exe", "notepad", 0, 0, "." ) or die Win32ProcessError(); print("Waiting...\n"); $ProcessObj->Wait(INFINITE); print("Done.\n"); }

    works fine for me.

    Maybe mwebprep spawns a child an exits immediately? Compare $ProcessObj->GetProcessID() with the PID you see in the Task Manager.

      Thanks, I'll check this. I notice you are importing INFINITE explicitly, but I wasn't. That could be it too, although it's supposed to be exported automatically. Thanks!
        Nope, I made sure it was exported by default. I just like listing my imports.
Re: Win32::Process not waiting
by ikegami (Patriarch) on Feb 15, 2007 at 16:11 UTC

    My Perl parent keeps processing

    That's odd, since there's nothing after the Wait. How can you even tell?

      Thanks. My comment "# more code" was intended to show that there was additional stuff there but not included.
        Please don't show us one program and talk about another. What problem does the program you posted exhibit?
      I just had another thought, rereading your replies. Maybe my misunderstanding is even worse than I thought. Isn't the parent program supposed to wait at $ProcessObj->Wait(INFINITE)? In other words, whatever happens after that line should wait until the child finishes. Is that right?

      If I'm wrong about that, it would explain my confusion!
Re: Win32::Process not waiting
by zentara (Cardinal) on Feb 16, 2007 at 12:52 UTC
    I don't use windows, but I find that while(1) loop and multiple sleeps very suspicious. They will definitely screw up a Tk event loop. Can you rewrite your code to use Tk timers instead of while(1) and sleep?

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum