instance continues running even after being closed by a thread

Asked

Viewed 29 times

0

Good even after closing my instance it keeps running because of a thread I created to call a while:

public void nativeKeyPressed(NativeKeyEvent event) {
        System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(event.getKeyCode()));
        if (event.getKeyCode() == NativeKeyEvent.VC_BACKQUOTE) {
            keepPressingO = true;
            new Thread() {
                @Override
                public void run() {
                    try {
                        while (keepPressingO) {
                             Robot robot = new Robot();
                             robot.keyPress(KeyEvent.VK_Q);
                             Thread.sleep(26);
                             robot.keyRelease(KeyEvent.VK_Q);
                             robot.keyPress(KeyEvent.VK_W);
                             Thread.sleep(16);
                             robot.keyRelease(KeyEvent.VK_W);
                             robot.keyPress(KeyEvent.VK_E);
                             Thread.sleep(26);
                             robot.keyRelease(KeyEvent.VK_E);
                        }

                    } catch (AWTException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }.start();

        }

        if (event.getKeyCode() == NativeKeyEvent.VC_ENTER) {
            keepPressingO = false;
        }

    }

Could someone help me as I resolve this situation? How could I thread her to sleep when she left the while ( and if she went back to if she would wake up?)

And how could I call that thread inside a windowClosing to kill her?

1 answer

0

tries to create this thread outside that scope it in the class variable declaration, and finally to finish it try the Join method().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.