Skip to content

Instantly share code, notes, and snippets.

@come25136
Last active August 1, 2016 01:51
Show Gist options
  • Save come25136/13a78a5f615206c2d075fdc4849e275d to your computer and use it in GitHub Desktop.
Save come25136/13a78a5f615206c2d075fdc4849e275d to your computer and use it in GitHub Desktop.
Java Process exit Detection code.
/**
* @author come25136
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
class watch {
static boolean pidwatch(int pid, int time) {
final boolean[] timeout = {false};
ArrayList<String> list = new ArrayList();
new Thread(() -> {
try {
Thread.sleep(time);
timeout[0] = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
try {
while (!timeout[0]) {
BufferedReader br = new BufferedReader(new InputStreamReader(new ProcessBuilder("tasklist").start().getInputStream()));
for (String line = br.readLine(); line != null; line = br.readLine()) {
if (!line.isEmpty()) { list.add(line.replaceAll("\\s+", ",").split(",", -1)[1]); }
}
if (!list.contains(Integer.toString(pid))) {
return true;
}
list.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment