I'm looking for a way to keep checking the status of a Windows Service in a loop until it has fully stooped or started.
I used Malik Ehtasham code Here to get the status of the service. But I have to keep pressing a button to get the status. I would like for it to just keep checking status saying... please wait.. then say stooped or started.
This is what i have to start the service:
String[] StartSpooler = {"cmd.exe", "/c", "sc", "start", "spooler"};
try {
Process process = new ProcessBuilder(StartSpooler).start();
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String StartSpoolerLine;
while ((StartSpoolerLine= bufferedReader.readLine()) != null) {
System.out.println(StartSpoolerLine);
}
} catch (Exception ex) {
System.out.println("Exception : " + ex);
}
This is what I have to keep checking the service status but its not working
try {
Process p = Runtime.getRuntime().exec("sc query spooler");
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
if (line.trim().startsWith("STATE")) {
while (!"4".equals(line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim())) {
System.out.println("Starting....");
}
System.out.println("Service is started and running");
}
line = reader.readLine();
}
} catch (IOException e1) {
}
Please help, thanks!