Hi,

"Shashank Date" <ADATE / kc.rr.com> wrote in message
news:XCF89.44011$mj7.808228 / twister.rdc-kc.rr.com...
>
> Well, if you change the code back to it's first version where:
>
> > > cmds = [
> > >         "ping 10.0.0.0 -n 30 -w 1000 > NUL",
> > >         "ping 10.0.0.0 -n 60 -w 1000 > NUL",
> > >         "ping 10.0.0.0 -n 90 -w 1000 > NUL"
> > >        ]
>
> you will see that the threads are run sequentially and the program ends
> after (30 + 60 + 90) seconds.
> This is probably because the f.gets in the "while not f.eof?" loop for
each
> thread stalls.
>
> See what I mean ?
Yes, I see.

Here is new version.
Caution: Do not use redirection like "ping 10.0.0.0 -n 30 -w 1000 > NUL".

#========================================================
require 'Win32API'

OpenProcess = Win32API.new("kernel32","OpenProcess",['L','L','L'],'L')
TerminateProcess = Win32API.new("kernel32","TerminateProcess",['L','L'],'L')
WaitForSingleObject =
Win32API.new("kernel32","WaitForSingleObject",['L','L'],'L')

 puts 'Start: ' + `echo %TIME%`
 cmds = [
         "ruby -e 'sleep 30'",   # long running processes with no output
         "ruby -e 'sleep 60'",   # long running processes with no output
         "ruby -e 'sleep 90'"    # long running processes with no output
        ]

 out = []
 threads = []

 for c in cmds
     threads << Thread.new(c) { |myCmd|
         puts "#{myCmd}"
         f = IO.popen(myCmd)
         hProcess = OpenProcess.Call(0x100001, 1 ,f.pid)
         Thread.current['pid'] = hProcess
         while true
           a = WaitForSingleObject.Call( hProcess, 0);
           break if a==0
         end
     }
 end

 puts threads.length.to_s + ' threads running'

 begin
     i = 0
     threads.each { |t| i += 1 if t.stop? }
     puts `echo %TIME%`
     b = File.exists?("C:junkj0.txt")
     puts i.to_s + ':' + File.mtime("C:junkj0.txt").to_s if $DEBUG and b
     $stdout.flush
 end until (i == threads.length) || (not b)
 puts 'Mid: ' + `echo %TIME%`
 threads.each { |t|
    TerminateProcess.Call(t['pid'],0) if t.alive?
 }

 puts 'Stop: ' + `echo %TIME%`
 puts out if $DEBUG
#===========================================

Park Heesob.