# WIN32OLE\_EVENT causes cpu running 100%

**URL:** <https://www.ruby-forum.com/t/win32ole-event-causes-cpu-running-100/108514>\
**Category:** Ruby\
**Created:** [July 4, 2007, 10:15am UTC](https://www.ruby-forum.com/t/win32ole-event-causes-cpu-running-100/108514 "2007-07-04T10:15:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [July 4, 2007, 10:15am UTC](https://www.ruby-forum.com/t/win32ole-event-causes-cpu-running-100/108514/1 "2007-07-04T10:15:31Z")

</div>

Hello!

I have some code do catch some events from MS Outlook.  
Especially TastItem events.

But my problem is, when catching the events, the CPU is running at 100%.  
Is there any chance to do it in an other way?

My code is here:

* * *

class OutlookEvents

include Singleton

OL\_FOLDER\_TASKS = 13

OL\_TASK\_NOT\_STARTET = 0  
OL\_TASK\_IN\_PROGRESS = 1  
OL\_TASK\_COMPLETE = 2  
OL\_TASK\_WAITING = 3  
OL\_TASK\_DEFERRED = 4

def initialize()  
begin  
@ol = WIN32OLE.connect(“Outlook.Application”)  
rescue  
@ol = WIN32OLE.new(“Outlook.Application”)  
end  
end

def default\_handler(event, \*args)  
puts event  
puts args  
end

def handle\_task\_events()  
appNS = @ol.Application.GetNamespace(“MAPI”)  
appTasks = appNS.GetDefaultFolder(OL\_FOLDER\_TASKS)  
appItems = appTasks.Items

```
appEvents = WIN32OLE_EVENT.new(@ol, "ApplicationEvents")

appItems.each do |item|
  if(item.Status != OL_TASK_DEFERRED) && (item.Status !=

```

OL\_TASK\_COMPLETE)  
events = WIN32OLE\_EVENT.new(item, “ItemEvents”)

```
    #event = events.on_event() do |*args|
    event = events.on_event("Open") do |*args|
      puts " ... got item events!"
      sleep 0.5
      default_handler(event, *args)
      #sleep 3
      # stop_listening = true
    end
  end
end

stop_listening = false

appEvent = appEvents.on_event("Quit") do |*args|
  puts " ... got application events!"
  sleep 0.5
  default_handler(appEvent, *args)
  stop_listening = true
end

WIN32OLE_EVENT.message_loop until stop_listening

```

End

outlook = OutlookEvents.instance  
outlook.handle\_task\_events()  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+

Thnx, Michael
