def handle_events(timeout=nil, completion_flag=nil)
      if completion_flag && !completion_flag.is_a?(Context::CompletionFlag)
        raise ArgumentError, "completion_flag is not a CompletionFlag"
      end
      if timeout
        timeval = Call::Timeval.new
        timeval.in_ms = timeout
        res = if Call.respond_to?(:libusb_handle_events_timeout_completed)
          Call.libusb_handle_events_timeout_completed(@ctx, timeval, completion_flag)
        else
          Call.libusb_handle_events_timeout(@ctx, timeval)
        end
      else
        res = if Call.respond_to?(:libusb_handle_events_completed)
          Call.libusb_handle_events_completed(@ctx, completion_flag )
        else
          Call.libusb_handle_events(@ctx)
        end
      end
      LIBUSB.raise_error res, "in libusb_handle_events" if res<0
    end
def handle_events(timeout=nil, completion_flag=nil)
      if completion_flag && !completion_flag.is_a?(Context::CompletionFlag)
        raise ArgumentError, "completion_flag is not a CompletionFlag"
      end
      if timeout
        timeval = Call::Timeval.new
        timeval.in_ms = timeout
        res = if Call.respond_to?(:libusb_handle_events_timeout_completed)
          Call.libusb_handle_events_timeout_completed(@ctx, timeval, completion_flag)
        else
          Call.libusb_handle_events_timeout(@ctx, timeval)
        end
      else
        res = if Call.respond_to?(:libusb_handle_events_completed)
          Call.libusb_handle_events_completed(@ctx, completion_flag )
        else
          Call.libusb_handle_events(@ctx)
        end
      end
      LIBUSB.raise_error res, "in libusb_handle_events" if res<0
    end
Handle any pending events in blocking mode.

 This method must be called when libusb is running asynchronous transfers.
 This gives libusb the opportunity to reap pending transfers,
 invoke callbacks, etc.

 If a zero timeout is passed, this function will handle any already-pending
 events and then immediately return in non-blocking style.

 If a non-zero timeout is passed and no events are currently pending, this
 method will block waiting for events to handle up until the specified timeout.
 If an event arrives or a signal is raised, this method will return early.

 If the parameter completion_flag is used, then after obtaining the event
 handling lock this function will return immediately if the flag is set to completed.
 This allows for race free waiting for the completion of a specific transfer.
 See source of {Transfer#submit_and_wait} for a use case of completion_flag.

 @param [Integer, nil] timeout  the maximum time (in millseconds) to block waiting for
                                events, or 0 for non-blocking mode
 @param [Context::CompletionFlag, nil] completion_flag  CompletionFlag to check

 @see interrupt_event_handler