Stopping main program flow while a handler is active

Asked by Ned

For example, lets say we have a simple Script like:

(1)def myHandler(event):
(2) #code to make popup vanish
(3) event.region.stopObserver()

(4)def func():
(5) #Main Function code
(6) onAppear(pImage, myHandler)
(7) observe(60,background=True)
(8) wait(5)
(9) #Continue with main function

The problem i'm having is that the image i'm observing for won't necessarily always appear during automation of a particular GUI app - in fact most of the time it won't. The only way I can get this to work is to to put a long wait between the observer action area and the continued flow of the program (9) This however means a needless 5 second pause every time automation is executed and that image/window does not appear. I have tried using various boolean flags but to no avail. Does anyone else have any experience with this?

Thank you

Question information

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

if you know the max waiting time for the popup, then this would do the job:

(1)def myHandler(event):
(2) #code to make popup vanish
(3) event.region.stopObserver()

(4)def func():
(5) #Main Function code
(6) onAppear(pImage, myHandler)
(7) observe(5)
(9) #Continue with main function

Now you are using the inline observe, which would continue with (9) immediately after the popup appeared but max 5 seconds (if not).

usually one cannot really proceed with the main workflow while having the risk of a popup.
But if it is possible in your case:
(1)def myHandler(event):
(2) #code to make popup vanish
(3) event.region.stopObserver()

(4)def func():
(5) #Main Function code
(6) onAppear(pImage, myHandler)
(7) observe(5,background=True)
(9) #Continue with main function

it continues immediately after (7), since the observe is done in parallel and stopped after 5 seconds anyway.

If you can somehow foresee the popup situation, this would be also possible (no use of observe):

** not needed (1)def myHandler(event):

(4)def func():
(5) #Main Function code
if popupMightAppear:
    if exists(pImage, 5):
        #code to make popup vanish
(9) #Continue with main function

Be aware: up to version 1.0.1 the observe feature has many quirks and the most critical is, that mouse and keyboard usage is not coordinated between main workflow and handler in case of background=true, which might lead to strange situations.

with version 1.1.0+ mouse/keyboard usage is coordinated between threads.
Additionally for critical sections it is possible to reserve the usage for one thread.
The background observe will have a feature to check in main workflow wether the observed events have already happened, which allows, to have the handling inline (no handler might be needed) at various places in the workflow, which is the best way to coordinate.

Revision history for this message
Ned (nedleonard) said :
#2

Thanks Raiman! A simple solution that I was too muddled up to see.