Observer not stopping / causing script to a halt/slugghish execution

Asked by xyz_User on 2020-03-10

Please advise how I can properly write this better. It seems to be working but after a few min. The script seems to sit idle.
I don't even think the observer is stopping correctly.

I'm monitoring a specified region for changes (lots of movement in that region) and when it's found, clicks that region then continues with the script.

Here's what I have:

def boxObserve():
    def changed(event):
      event.stopObserver()
      sleep(0.15)
      box.stopObserver()
      for chg in event.changes:
        box.stopObserver()
        sleep(0.25)
        event.changes[-1].click() #click on the last changed agrea
      doSomethingAfterClicking() #Do this after clicking
    box = Region(684,552,266,371)
    box.onChange(500, changed)
    box.observe(0.5,background=False)
    sleep(0.25)
    box.stopObserver()
    doSomethingIfNothingChanged() #Do this function if Observer didnt detect any changes.

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2020-03-11
Last query:
2020-03-11
Last reply:
2020-03-10

This question was reopened

Best RaiMan (raimund-hocke) said : #1

looks like some stopObserver overkill ;-)

my suggestion:

def boxObserve():
    def changed(event):
        global someChanges
        event.changes[-1].click() #click on the last changed area
        someChanges = True
        event.stopObserver()
    box = Region(684,552,266,371)
    box.onChange(500, changed)
    someChanges = False
    box.observe(0.5,background=False)
    if someChanges:
        doSomethingAfterClicking() #Do this after clicking
    else:
        doSomethingIfNothingChanged() #Do this function if Observer didnt detect any changes.

some comments:
- in a handler should not be lengthy activities, because the main thread is paused meanwhile
- when the observe time is reached, the observer is stopped anyways
- when the handler is visited, event.changes contains at least one entry
- global variables can be used to communicate between handler and main thread

hope it helps

xyz_User (userseven) said : #2

Thanks RaiMan, that solved my question.