-2

I know time and time again people have asked how to start a thread after it's been stopped and everyone says you can't. This isn't a duplicate to that because I've found no solution for the problem.

private void runInBackground() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            while (running) {
                try {
                    checkPixel();
                } catch (AWTException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

@Override
public void nativeKeyPressed(NativeKeyEvent e) {
    // TODO Auto-generated method stub
     System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
     if(NativeKeyEvent.getKeyText(e.getKeyCode()).equals("F9")){
         stop();
     }
     else if(NativeKeyEvent.getKeyText(e.getKeyCode()).equals("F10")){

     }

So in my code I'm listening for global key events using JNativeHook. I can successfully stop the checkPixels() using the F9 key but I'm not understanding what I should do using F10 when I wanna start up checkPixel() again.

checkPixel() basically checks for a change in pixel color

ANSWERED Added an if statement for my state variable running and keep the while loop true allows me to turn on/off the method while keeping the thread open. Thank you Jaboyc

    private void runInBackground() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                if(running){
                    try {
                        checkPixel();
                    } catch (AWTException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }).start();
}
Huang Chen
  • 1,177
  • 9
  • 24
  • I don't fully understand what your code is trying to do, but why can't you just call `runInBackground()` again? – Ray Jul 29 '15 at 15:56
  • 1
    What does the `stop()` method do? You should be changing `running` to true/false atomically – Vince Jul 29 '15 at 15:56
  • 1
    possible duplicate of [How pause and then resume a thread?](http://stackoverflow.com/questions/16758346/how-pause-and-then-resume-a-thread) (I know you said it wasn't a duplicate, but it actually is. The functionality you want has already been requested) – Vince Jul 29 '15 at 16:03
  • @Ray calling runInBackground() for some reason does not work, I've tried – Huang Chen Jul 29 '15 at 16:20
  • @VinceEmigh The stop() method sets the variable in the while loop, running, to false – Huang Chen Jul 29 '15 at 16:20

1 Answers1

2

Would this work

while (true) {
    if (running) {
        doStuff();
    }
}

in the run method?

Jaboyc
  • 567
  • 1
  • 8
  • 16
  • That's the same as `while(running) doStuff()`, which he already has. He isn't showing us whether he actually changes the value of it or not. I suggest deleting this answer before downvotes come – Vince Jul 29 '15 at 16:11
  • wow this actually works since it keeps the thread running at all times, thank you so much. And wow sophomore in high school eh? impressive, smarter than my senior in college-self :) thank you again, really neat simple fix – Huang Chen Jul 29 '15 at 16:23