How to pause the main script when handling an event using observeInBackground

Asked by edlothiad on 2017-12-06

I've been attempting to stop the script while handling my events when using observeInBackground, and from this answer here: you've mentioned this isn't possible but is hopefully in the works for Sikuli 2.

I wanted to know if you could provide any advice for a work around?

You mentioned (in the linked answer) using a global variable, but if I'm expecting to be able to preform several finds in that period and an unexpected popup appears, would there be a "clever" or more efficient way of protecting the script?

Would using try statements with a check in the except be practical (they'd have to wrap almost everything if it was a random popup, no?) or testing for the global variable before clicks?

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

-- solution 1:
define at the beginning of main script:
shouldWait = False

Make a function like:
def myFind(region, imageOrPattern, waittime = 3):
    while shouldWait:
    return region.wait(imageOrPattern, waittime)

... and use it instead of the find operations, that needs to be guarded.

instead of

click(myFind(SCREEN, someImage))

... and in the observe handler at the beginning:
global shouldWait
shouldWait = True
# do your handling
shouldWait = False

This is a basic solution, that might help in many cases. The risk is, that the handler event is triggered, but the handler not yet started and a find op has already begun.

-- solution 2:
Intercept the FindFailed situations, that are caused by the GUI event (e.g. popup hiding the target image), that should be handled by observeInBackground.
Since version 1.1.1 we have a callback feature for FindFailed situations:

Similar to solution 1 you have to define and handle global variables for communication between the FindFailed handler and the observe handler.

The principal workflow:
- in FindFaild handler check, wether an observe event is or was handled
- if yes, wait for the completion of the observe handling and return with the REPEAT advice
- if no, then you have a normal FindFailed that should be handled as needed

This solution is a bit more complex, but has a chance, to get around all such conflict situations.

edlothiad (edlothiad) said : #2

These solutions sound perfect, I see why you prefer it to be asked as it's own question rather than as a comment thread.

I unfortunately can't test them right now, however will test both and see how they work, but from first looks it would seem like Solution 1 would work fine but solution 2 might be the more robust.
Also thank you for the information on the findfailed exception handling!

RaiMan (raimund-hocke) said : #3

no problem. take your time.

Can you help with this problem?

Provide an answer of your own, or ask edlothiad for more information if necessary.

To post a message you must log in.