[Win32utils-devel] Taking a stab at a native windows timeout method

Berger, Daniel
2008-02-25T19:52:45Z

Hi all,

Charles O. Nutter posted an interesting entry regarding timeout.rb

http://headius.blogspot.com/2008/02/rubys-threadraise-threadkill-timeout
rb.html

Based on one of the followup comments I tried this:

require ''windows/synchronize''
require ''win32/event''

def wtimeout(sec, exception=Error)
   return yield if sec == nil or sec.zero?
   raise ThreadError, "timeout within critical session" if
Thread.critical
   begin
      terminate_event = Win32::Event.new
      x = Thread.current
      y = Thread.start {
         result = WaitForSingleObject(terminate_event.handle, sec *
1000)
         if x.alive? && result == Win32::Event::TIMEOUT
            x.raise exception, "execution expired"
         end
      }
      yield sec
   ensure
      if y and y.alive?
         terminate_event.set
      end
   end
end

wtimeout(2){
   puts "Starting"
   sleep 5
   puts "Shouldn''t reach here"
}

But, it doesn''t seem to work. Any suggestions?

Thanks,

Dan


This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly
prohibited and may be unlawful.  If you have received this communication
in error, please immediately notify the sender by reply e-mail and destroy
all copies of the communication and any attachments.