Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Win32::Process Question 1

Status
Not open for further replies.

JRyanCon

Technical User
Aug 19, 2004
47
US
I am trying to figure out how to get 2 subrutines called at the same time using Win32::process. It seems like it should do what I want it to do. But I cant figure out the syntax. The example give:

============

use Win32::process;
Win32::process::Create($ProcessObj,
"c:\\windows\\system32\\notepad.exe",
"notepad",
0,
NORMAL_PRIORITY_CLASS,
".")|| die ErrorReport();

=============

Now this works to call the program. But I cant seem to get the syntax for callin a &Sub. What I want is something like this:

==============
use Win32::process;
use File::Copy;

sub move_file {
file copy file.txt renamed.txt;
}

sub hello {
print "hello world\n";
}



and then to call both &Subs at the same time. Then I want to have &Hello wait till &move is done and then have them loop over and over X times.


Any ideas?

Thanks

Ryan
 
Hello ryan,

I think that Perl threads will do what you need.

use threads; $thr = threads->new(\&sub1);

@ReturnData = $thr->join;
print "Thread returned @ReturnData";

sub sub1 { return "Fifty-six", "foo", 2; }

The, very short, example above starts a thread (running a subroutine, as you want it to) and then waits for it to rejoin the main thread.

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
ryan,

Do you have to use file::copy, spawning to system should do what you want, just fire your print before your move command
Code:
$loop;
while (@files) {
   $loop++;
   &print_it ($loop);
   &move_it($_, $oldpath, $newpath);   
}
sub print_it {
   ($count)=@_;
    print "$count is occurring\n";
}
sub move_it {
  ($file, $srcpath, $dstpath )=@_;
   system("move $srcpath$file $dstpath$file"); 
}

Doubt if that's what you wanted to do, but that's what it looks like ;-)

--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Paul --

haha oops ya incorrect syntax there. Thats the file copy command in TCL ;) sorry little bleed over from a different Scripting Lang. Nice catch.

Ryan
 
MikeLacey -

THANKS a million I think I have it doing what I want! I do have a question I would like cleared up however. so this is what I have ( stripted for simplicity of course )..

========
use threads;
use File::Copy;
use Digest::MD5;


my $md5Match="1";

sub copy ($) {
while ($md5Match == "1" ) {
# copy the file from D:/ to C:/....
}
}

sub check ($) {
while ($md5Match == "1" ) {
# do the md5 check when the file exists
}
}


$thr = threads->new(\&copy);
$thr2 = threads->new(\&check);

@ReturnData = $thr->join;
==================


Ok so this looks like it is running the 2 Subroutines at the same time. GREAT. My question is the "@ReturnData = $thr->join;"

The perldoc threads says
$thread->join
This will wait for the corresponding thread to join. When the thread finishes, join() will return the return values of the entry point function. If the thread has been detached, an error will be thrown.

Why is it I only have to join $thr and not $thr2 also? I tried it with:
@ReturnData = $thr->join;
@ReturnData = $thr2->join;

and it worked fine. I also ran it with just:
@ReturnData = $thr2->join;

and that also ran the same. Can you explain this "@ReturnData = $thr->join;" line for me?


Again Thanks alot!

Ryan
 
Hmmmm dunno - I'll do some reading. I remember something about it but I can't put my finger on it at the mo...

While I think about it - what are you returning from your two subs that means you need to say:

@ReturnData = $thr->join;

?

Or is that just a hangover from the example?

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
MikeLacey -

Ok I think I have a better understanding of this line now. Since I am actually not returning any values I can just use:
$thr->join;

As for why I didnt need:
$thr->join;
$thr2->join;

I think this is because I am actually ending both Threads at the same time so, in THIS situation I would only need to join one of them. Looks like the better programming practice would be to join each thread however.

Thanks again.

JRyan
 
That's my understanding as well J

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top