2

I'm trying to create Ruby script that allow me to get OLE events during execution of main script. I've moved WIN32OLE_EVENT::message_loop to another Thread and expect this will call my event handlers simultaneously during execution of other script parts. For example, main goal of this sample script is connect to device and get event messages (just puts it to stdout) during execution of irb console.

require 'win32ole'
require 'irb'

sc = WIN32OLE.new("OPOS")

sc.Open("mydevice")
sc.ClaimDevice(5000)
sc.setproperty("DeviceEnabled", true)
sc.DataEventEnabled = true
sc.AutoDisable = true

$loop = true

esc = WIN32OLE_EVENT.new(sc)
esc.on_event() {|*args|
  puts "SIGCAP Event #{args.inspect}"
  $loop = false
}

Thread.new {
    loop do
      WIN32OLE_EVENT::message_loop
      break unless $loop
      sleep 0.5
    end
}

IRB.start
WIN32OLE_EVENT::message_loop

When i execute this script (but you can't run it because some stuff skipped) i got irb console where i can access to objects that manipulate my USB device that connected to Windows and call some methods that fired different events and i expect these events inside esc.on_event block just to see output to stdout. But this block never called. But if i will write 'exit' in irb console i will get my events because last line in sample script WIN32OLE_EVENT::message_loop!

Looks like calling message_loop working only in main Thread and calling it inside other thread not give any results.

Any ideas how to get it working? Feel free to ask additional questions. Thanks.

4

0 回答 0