Could somebody please suggest me how I can pause a program and then resume it like what the pause/resume buttons on a media player do?
Thanks very much!
Could somebody please suggest me how I can pause a program and then resume it like what the pause/resume buttons on a media player do?
Thanks very much!
I guess you mean pausing some process then, - if the program waits for user action(s)
it already in a "dormant" stage.
You could have a button "Pause" that starts a thread, pauser, that does nothing but
sleep, and join() that thread. And another button "Resume", that does a
pauser.interrupt() call. In your run method's try/catch for InterruptedException you
then start/resume the process by a call to an appropriate method and do a return
from the run method.
Thanks a lot, sjalle.
Before I come to this step, I am now stuck on another problem.
I have a main method, I let it run and I get a GUI interface, at this stage, I want it to wait until I press a "Start" button and then the program could start to run something, listen to the pause and resume interrrupt.
I now made my progarm something like this:
public static void main( String args[]){
.....
xx1.start();
}
.
.
.
public void start(){
try{
wait();
}catch(InterruptedException e){
xx2.programRun());
}
}
And I got the following exception:
java.lang.reflect.InvocationTargetException:
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method).
Could somebody suggest me on it please? Thanks!
Hmm, I don't think you need a thread for that. Just a method that starts the
processing (loop ?) and checks for the value value of a boolean (goOn ?) that
is set to true/false at the click of your buttons. So your processing is started/
continued in the Start buttons eventhandler that sets goOn=true and starts/continues
the processing, and the Pause btn that sets goOn=false, which in turn will break the
processing loop.