Refer to http://msdn2.microsoft.com/fr-fr/library/ms686722.aspx,
“While open handles to kernel objets are closed automatically when a
process
terminates, the objects themselves exist until all open handles to them
are
closed. Therefore, an object will remain valid after a process that is
using
it terminates if another process has an open handle to it.”
Here is the working code.
irb(main):001:0> require ‘win32/process’
=> true
irb(main):002:0> include Windows::Handle
=> Object
irb(main):003:0> info = Process.create :app_name=>‘notepad’
=> #
irb(main):004:0> Process.kill 0,info.process_id
=> [1748]
irb(main):005:0> Process.kill 9,info.process_id
=> [1748]
irb(main):006:0> CloseHandle(info.process_handle)
=> true
irb(main):007:0> CloseHandle(info.thread_handle)
=> true
irb(main):008:0> Process.kill 0,info.process_id
ProcessError: The parameter is incorrect.
from
c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
s.rb:156:in kill' from c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces s.rb:132:in each’
from
c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
s.rb:132:in `kill’
from (irb):8
irb(main):004:0> Process.kill 0, info.process_id
Any ideas?
Here is the working code.
=> [1748]
s.rb:156:in kill' from c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces s.rb:132:in each’
from
c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
s.rb:132:in `kill’
from (irb):8
So, if I use Process.create to create a new process, and I want to
know when a user closed the program, I should close the handle to the
process immediately after starting it? And then Process.kill(0,
) would return an exception once the user closed the program?
Here is the working code.
=> [1748]
s.rb:156:in `kill’
process immediately after starting it? And then Process.kill(0,
) would return an exception once the user closed the program?
Joe
You cannot close the hanle if the process is still alive. You can use
WaitForSingleObject or Process.waitpid to wait until child process exits
like this:
require ‘win32/process’
include Windows::Handle
info = Process.create :app_name=>‘notepad’
Process.waitpid(info.process_id)
CloseHandle(info.process_handle)
CloseHandle(info.thread_handle)
using
process
ProcessError: The parameter is incorrect.
require ‘win32/process’
include Windows::Handle
info = Process.create :app_name=>‘notepad’
Process.waitpid(info.process_id)
CloseHandle(info.process_handle)
CloseHandle(info.thread_handle)
If I use Process.waitpid in a thread, all the other threads seem to
block until the process that I’m waiting for stops.
I’m writing a program that runs on a windows machine that listens for
requests to start and kill programs. It needs to be able to run and
kill multiple programs and needs to be able to monitor the programs
that it has started to make sure that they are still running. If
Process.waitpid makes the whole Ruby process hang, that’s no good.
irb(main):004:0> Process.kill 0, info.process_id
Any ideas?
Here is the working code.
=> [1748]
s.rb:156:in kill' from c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces s.rb:132:in each’
from
c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
s.rb:132:in `kill’
from (irb):8