Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Win32::CreateProcess problem

by mdamazon (Acolyte)
on Apr 19, 2018 at 02:32 UTC ( [id://1213148]=perlquestion: print w/replies, xml ) Need Help??

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

I can execute Win32::CreateProcess in a loop succesfully. Now I want to prevent my script from moving forward until all the child processes that were created in the loop have exited. This is causing problems. The code inside the first foreach loop below executes properly. The code below inside the while loop which attempts to detect the status of the child processes is not.

use Win32::Process qw(CREATE_NEW_CONSOLE STILL_ACTIVE); use Win32; my %procs=(); foreach my $a (@array) { my $cmd01 = 'some command ' . $a . ' other stuff'; my $cmd = "cmd /c echo Performing job. Please wait... +& $cmd01"; my $ProcessObj; Win32::Process::Create($ProcessObj,"C:\\WINDOWS\\syste +m32\\cmd.exe","$cmd01",0,CREATE_NEW_CONSOLE,"."); $procs{$ProcessObj} = $a; } while (%procs) { foreach my $key (keys %procs) { my $exit_code = 1; $key->GetExitCode($exit_code); while($exit_code == Win32::Process::ST +ILL_ACTIVE()) { sleep(1); $key->GetExitCode($exi +t_code); } print "GetExitCode returned $exit_code +, exiting main process for $procs{$key}.\n"; delete $procs{$key}; } }

This returns:

Can't locate object method "GetExitCode" via package
"Win32::Process=SCALAR(0x6e604a4)" (perhaps you forgot to load "Win32::Process=SCALAR(0x6e604a4)"?) at ..\code\script.pl line 1498, <STDIN> line 1 (#1)
Uncaught exception from user code:
Can't locate object method "GetExitCode" via package "Win32::Process=SCALAR(0x6e604a4)" (perhaps you forgot to load "Win32::Process=SCALAR(0x6e604a4)"?) at ..\code\script.pl line 1498, <STDIN> line 1.

Replies are listed 'Best First'.
Re: Win32::CreateProcess problem
by ikegami (Patriarch) on Apr 19, 2018 at 03:45 UTC

    Hash keys can only be strings.

    Change

    $procs{$ProcessObj} = $a; keys %procs delete $procs{$key};
    to
    $procs{ $ProcessObj->GetProcessID() } = $ProcessObj; values %procs delete $procs{ $ProcessObj->GetProcessID() };
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1213148]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-24 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found