need simple lock code to keep event handler in line

Asked by Rosie Bell

Hi guys,

This relates to a previous question raised by Josh 183655.

I have an event handler to click an OK button on an error messagebox that pops up.
It seems to always happen in the middle of an activity where I swipe a field and read its contents into the clipboard.
The result is that I end up with the error pop up message on the clipboard rather than the value I want to be captured by my Cntl C action.

Do you have any simple lock code that I could put around my swipe function to make it atomic or my event handler?
I have tried using import threading and using the threadlock.acquire() and threadlock.release() functions. Am I on the right track??

My rogue functions are.....

def swipe(begin,end):
    # acquire lock needed here?
    print "in swipe"
    dragDrop(begin,end)
    type("c",KEY_CTRL)
   #release lock here?

def ClickOK(event):
   # get lock here?
    print "In handler"
    event.region.click("Error.png") # clicks the ok button on the pop up
    # release lock here?

Many thanks for any help,
Rosie

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
RaiMan (raimund-hocke) said :
#1

I think, this is not the whole story, since this does not prevent the popup to arise in parallel with the swipe action, which is the real problem: it gets to foreground and accepts the swipe action and the content goes to the clipboard.

I think you have to check, wether the popup arises during this sequence and decide to reset and repeat the swipe action.

Your current locking prevents the swiping, if the handler is already active. But if the swipe has locked and started, the shit might happen any way and on top blocks the handler to do its job.

I would not use the locking feature, but something like this:

first the handler:

def ClickOK(event):
    Settings.myInHandler = True
    event.region.click("Error.png") # clicks the ok button on the pop up
    wait(0.5)
    Settings.myInHandler = False

def swipe(begin,end):
    while True:
        while Settings.myInHandler: wait(0.5)
        mouseDown(begin, Button.LEFT)
        if Settings.myInHandler: continue
        mouseMove(end)
        if Settings.myInHandler: continue
        mouseUp()
        if Settings.myInHandler: continue
        type("c",KEY_CTRL)
        wait(0.5)
        if Settings.myInHandler: continue
        break # if we finally come here, it should have worked.

I have broken the dragDrop into the 3 mouse actions to get more checkpoints in between.

Somewhere at the beginning you have to say:
Settings.myInHandler = False

the 2 wait()'s have to be adjusted accordingly, to get some overlapping for robustness.

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

BTW: I use Sikuli's Settings class as global variable storage - it is easier and more obvious

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

Just realized the possible orphaned mouseDown():

def swipe(begin,end):
    while True:
        while Settings.myInHandler: wait(0.5)
        mouseDown(begin, Button.LEFT)
        if Settings.myInHandler: mouseUp(); continue
        mouseMove(end)
        if Settings.myInHandler: mouseUp(); continue
        mouseUp()
        if Settings.myInHandler: continue
        type("c",KEY_CTRL)
        wait(0.5)
        if Settings.myInHandler: continue
        break # if we finally come here, it should have worked.

Revision history for this message
Rosie Bell (rbell-x) said :
#4

Yay! Had to tweak the delay a bit and changed the mouseDown(begin, Button.LEFT) into two commands as below and it is now swiping and copying like an expert! I know this might seem trivial but it gives me a 'go to' for event handling across the board and helps make things more robust. Thank you so much for all your help and the quick and comprehensive reply.
Rosie

def swipe(begin,end):
    while True:
        while Settings.myInHandler: wait(2)
        mouseMove(begin)
        mouseDown(Button.LEFT)
        if Settings.myInHandler: mouseUp(); continue
        mouseMove(end)
        if Settings.myInHandler: mouseUp(); continue
        mouseUp()
        if Settings.myInHandler: continue
        type("c",KEY_CTRL)
        wait(0.5)
        if Settings.myInHandler: continue
        break # if we finally come here, it should have worked.

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

LOL, .... I know this might seem trivial

--1. glad it helped

--2. this solution is rather sophisticated, since it needs some deeper understanding about how Sikuli works and what is possible and what is not (without inventing the wheel again ;-).
So welcome to the club of Sikuli experts ;-)

Always welcome.