lib/win32/event.rb in win32-event-0.5.0 vs lib/win32/event.rb in win32-event-0.5.1

- old
+ new

@@ -1,18 +1,22 @@ require 'win32/ipc' +# The Win32 module serves as a namespace only. module Win32 + + # The Event class encapsulates Windows event objects. class Event < Ipc # This is the error raised if any of the Event methods fail. class Error < StandardError; end extend Windows::Synchronize extend Windows::Error extend Windows::Handle - VERSION = '0.5.0' + # The version of the win32-event library + VERSION = '0.5.1' # The name of the Event object. # attr_reader :name @@ -96,24 +100,28 @@ # options for manual_reset and initial_state cannot be set (since they # are already set). Also, this method will raise an Event::Error if the # event doesn't already exist. # # If you want "open or create" semantics, then use Event.new. - #-- - # The OpenEvent() call here is strictly to force an error if the user - # tries to open an event that doesn't already exist. # def self.open(name, inherit=true, &block) if name && !name.is_a?(String) raise TypeError, 'name must be a string' end bool = inherit ? 1 : 0 - handle = OpenEvent(EVENT_ALL_ACCESS, bool, name) - if handle == 0 || handle == INVALID_HANDLE_VALUE - raise Error, get_last_error + + # This block of code is here strictly to force an error if the user + # tries to open an event that doesn't already exist. + begin + handle = OpenEvent(EVENT_ALL_ACCESS, bool, name) + + if handle == 0 || handle == INVALID_HANDLE_VALUE + raise Error, get_last_error + end + ensure + CloseHandle(handle) if handle > 0 end - CloseHandle(handle) self.new(name, false, false, inherit, &block) end # Returns whether or not the object was opened such that a process