2

I have simple sikuli wait operation in a background thread:

public static void main(String[] args) {
    Runnable rn = () -> {
        Screen s = new Screen();
        try {
            s.wait(imgPattern, 5);
        } catch (FindFailed e) {
            e.printStackTrace();
        }
        System.out.println("Finished wait.");
    };

    Thread th = new Thread(rn);
    th.start();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e1) {
    }

    th.interrupt();
    try {
        th.join();
    } catch (InterruptedException e) {
    }
    System.out.println("Finished main.");
}

When i try to interrupt and stop background thread i got java.lang.InterruptedException, but sikuli still works. This is output of the program:

java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at java.awt.Robot.delay(Unknown Source)
    at org.sikuli.script.RobotDesktop.delay(RobotDesktop.java:240)
    at java.awt.Robot.autoDelay(Unknown Source)
    at java.awt.Robot.afterEvent(Unknown Source)
    at java.awt.Robot.mouseMove(Unknown Source)
    at org.sikuli.script.RobotDesktop.doMouseMove(RobotDesktop.java:61)
    at org.sikuli.script.RobotDesktop.smoothMove(RobotDesktop.java:165)
    at org.sikuli.script.RobotDesktop.smoothMove(RobotDesktop.java:145)
    at org.sikuli.script.Mouse.move(Mouse.java:361)
    at org.sikuli.script.Mouse.move(Mouse.java:331)
    at org.sikuli.script.Mouse.init(Mouse.java:60)
    at org.sikuli.script.Screen.initScreens(Screen.java:107)
    at org.sikuli.script.Screen.<clinit>(Screen.java:72)
    at SikuliTest.lambda$0(SikuliTest.java:12)
    at java.lang.Thread.run(Unknown Source)

FindFailed:     /C:/imgPatternPath.png: (18x14) in S(0)[0,0 1366x768] E:Y, T:3,0
  Line 2759, in file Region.java

    at org.sikuli.script.Region.wait(Region.java:2759)
Finished wait.
Finished main.
    at SikuliTest.lambda$0(SikuliTest.java:14)
    at java.lang.Thread.run(Unknown Source)

How do i properly stop sikuli from working?

hal
  • 831
  • 3
  • 13
  • 32

2 Answers2

1

There might be some confusion in regards to how interrupt works. Quoting from this answer,

Thread.interrupt() sets the interrupted flag of the target thread. Then code running in that target thread MAY poll the interrupted status and handle it appropriately. Some methods that block such as Object.wait() may consume the interrupted status immediately and throw an appropriate exception (usually InterruptedException)

Interruption in Java is not pre-emptive. Put another way both threads have to cooperate in order to process the interrupt properly. If the target thread does not poll the interrupted status the interrupt is effectively ignored.

Polling occurs via the Thread.interrupted() method which returns the current thread's interrupted status AND clears that interrupt flag. Usually the thread might then do something such as throw InterruptedException.

This means that Sikuli wait probably does not handle the interrupt properly (not polling the interrupted status) and keeps running normally.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
0

Actually Sikuli uses Robot.delay(), which - unlike Thread.sleep() - ignores InterruptedException.

What you can do is to create a Process to run Sikuli automation, and you can terminate it by calling sikuliProcess.destroy()

Nin
  • 375
  • 1
  • 11