← Back to team overview

sikuli-driver team mailing list archive

[Question #293847]: How to gracefully interrupt running script when using SwingWorker and JFrame?

 

New question #293847 on Sikuli:
https://answers.launchpad.net/sikuli/+question/293847

Thanks to the help provided here I've created a JFrame to allow me more advanced user interaction and a custom control GUI. I only have one issue at this point, once the items runing in doInBackground (BgTask SwingWorker), you can not gracefully stop stop the script. I get the following error: java.lang.InterruptedException: sleep interrupted which I assume happens due to me trying to interrupt the 'wait' command. 

I tried to add a try/except to catch the exception, but it doesn't seem to work. Also when I try to use the CTRL+ALT+c hot key it stops the program and makes my JFrame and also the IDE unresponsive. 

I'm using Windows 7 64bit, I'm using SikulixIDE 1.1.0

CODE: 

from javax.swing import JButton, JFrame, JTextField, JPanel
from java.lang import Runnable, InterruptedException
from java.util.concurrent import ExecutionException
from javax.swing import SwingWorker, SwingUtilities
from java.lang import *

from java.awt.event import ActionListener, WindowAdapter
import threading

class BgTask(SwingWorker):
    def __init__(self, gui):
        self.gui = gui
        SwingWorker.__init__(self)

    def doInBackground(self):
        try:
            click("1463505602802.png") #Random icon on my desktop that is visible

            wait(5)

            click("1463514867811.png") #Random icon on my desktop that is visible
        except InterruptedException:
            print "***Automation Stuff Should End***"

    def done(self):
        try:
            self.get()
        except ExecutionException, e:
            raise SystemExit, e.getCause()
#             print e.getCause()

class Example: 
    def _setButtonStates(self, started):
        self.stopBtn.enabled = started
        self.startBtn.enabled = not started
    def stopSomething(self, event):
         self.bgTask.cancel(True)
         self.bgTask = None
         self._setButtonStates(started=False)
         
    def doSomething(self,event):
        self.bgTask = BgTask(self)
        self.bgTask.execute()
        self._setButtonStates(started=True)
            
        
    def __init__(self):
        frame = JFrame("Gamma Automated Testing")
        frame.setSize(200, 150)
        frame.addWindowListener(Adapter())
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
        pnl=JPanel()
        frame.add(pnl)
        self.textfield1 = JTextField('', 15)
        pnl.add(self.textfield1)        
        self.textfield2 = JTextField('', 15)
        pnl.add(self.textfield2)
        self.startBtn = JButton('Start Automation', actionPerformed=self.doSomething)
        self.stopBtn = JButton('Stop', actionPerformed=self.stopSomething)
        pnl.add(self.startBtn)
        pnl.add(self.stopBtn)        
        self.stopBtn.enabled = False        
        frame.pack()
        frame.setVisible(True)

endCondition = threading.Condition()
class Adapter(WindowAdapter):
    def windowClosing(self, event):
        endCondition.acquire()
        endCondition.notifyAll()
        endCondition.release()

class Runnable(Runnable):
    def __init__(self, runFunction):
        self._runFunction = runFunction

    def run(self):
        self._runFunction()
        
if __name__ == '__main__':
    SwingUtilities.invokeLater(Runnable(Example))
    endCondition.acquire()
#    Example()

    endCondition.wait()
    endCondition.release()
    

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.