It would be very nice if the
Read full article from Detecting process exit in Java | adrian's blog
Process
class would implement the Observable design pattern in some way. But it doesn’t and on this we will base our workaround creation.Process
contains a useful method for us, waitFor
, that pauses the current thread execution until the given subprocess is finished. So, we will create a thread that will be suspended until the given subprocess is finished. Then we will fire the listeners to notify that the process has finished its execution.public
void
run() {
try
{
// wait for the process to finish
process.waitFor();
// invokes the listeners
for
(ProcessListener listener : listeners) {
listener.processFinished(process);
}
}
catch
(InterruptedException e) {
}
}
processExitDetector =
new
ProcessExitDetector(subprocess);
processExitDetector .addProcessListener(
new
ProcessListener() {
public
void
processFinished(Process process) {
System.out.println(
"The subprocess has finished."
);
}
});
No comments:
Post a Comment