stop a reg.observeInBackground(FOREVER) whitout event happening

Asked by juan

windows 10, windows 7
sikuli 1.1.1 build 2017-03-29_10:41

I have a script
###

def test(event):
    Settings.reg.mouseMove(event.match)
    mouseDown(Button.MIDDLE)
    mouseUp(Button.MIDDLE)
    event.stopObserver()
def test1(event):
    ....

Settings.reg = Region(0,0,1279,1023)
Settings.reg.onAppear("1515499615589.png",test)
Settings.reg.onAppear("1515502700206.png",test)
Settings.reg.observeInBackground(FOREVER)

while True:
   MyMainCode....

###
I use forever because I do not know when the image will appear.
I have several small regions, and I observe them all at once.
On certain occasions, I need to stop the observation of all regions without the image appearing. I have to do this in mymaincode.
and then go back to observe, again in mymaincode
How can I do that?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

In your main code just issue a
Settings.reg.stopObserver()

for every region you want to stop its observing.

Revision history for this message
juan (fogelmanjg) said :
#2

thanks for response.

I tried with that line before asking ... but it does not stop observing
if I use the line event.stopObserver () inside the def: it works.
if I use the line event.stopObserver () in mymaincode it does not work and it says that event is not defined.
if I use the line Settings.reg.stopObserver () within the def or in mymaincode it does not work. keep observing.

Revision history for this message
RaiMan (raimund-hocke) said :
#3

ok, I will check as soon as possible.

Revision history for this message
juan (fogelmanjg) said :
#4

thanks for your time.
I'm going to try another pc to get rid of the doubt.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

tested with 1.1.2.

this works:

img = "img.png"

def handler(event):
  event.stopObserver()

onAppear(img, handler)
SCREEN.observeInBackground(FOREVER)

while True:
  wait(1)
  m = Mouse.at()
  if (m.x < 100 and m.y < 100): # condition 1
    SCREEN.stopObserver()
    wait(1)
  if not SCREEN.isObserving(): # condition 2
    break

- when the image gets visible, the script ends due to the stop in the handler and then condition 2

- when the image stays invisible after some time I move the mouse to top left corner of screen, which fires condition 1 and stops the observing and then the script ends due to condition 2

Revision history for this message
juan (fogelmanjg) said :
#6

### example
def test(event):
    SCREEN.mouseMove(event.match)
SCREEN.onAppear("1515531321687.png",test)
SCREEN.observeInBackground(FOREVER)
if SCREEN.isObserving():
    popup("is observing")
else:
    popup("is not observing")
SCREEN.stopObserver()
if SCREEN.isObserving():
    popup("is observing")
else:
    popup("is not observing")
while True:
   sleep(1)
###

With your example, I have created this other example, which has allowed me to solve my problem.
in my example if the image is on the screen when I run the script, move the mouse over the image and then show the popups, first is observing, second is not observing.
but if the image is not yet on the screen, it shows the popups, first is observing, second is not observing. If you show the image later, do not move the mouse.
in the first case the event is triggered before it can be stopped, when the image is already visible at the moment of starting the script, which is how it works.
I've tried it on my pc at home, which I have version 1.1.2 and it works fine.
in version 1.1.1 that I have at work (without internet to update) it does not work. I have to update it to version 1.1.2

thank you very much for your help.

Revision history for this message
juan (fogelmanjg) said :
#7

Thanks RaiMan, that solved my question.