I'm trying to make a program that will shutdown my computer after a certain time that I will input via GUI. I'm wondering if I did it the correct way.
Also, will this slow down my computer while it waits to shut off? Do you see anything wrong with the code?
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class PCShutdown {
private int minutes;
private int hours;
private void setHours(int hoursArg) {
this.hours = hoursArg;
}
private void setMinutes(int minutesArg) {
this.minutes = minutesArg;
}
private void shutDown() {
Thread shutDownThread = new Thread(new ShutDownThread());
shutDownThread.start();
}
private class ShutDownThread implements Runnable {
@Override
public void run() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec("shutdown -s -t 30");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} finally { process.destroy();}
}
}, (hours * 60 * 60 * 1000) + (minutes * 60 * 1000));
}
}
}
Also, I still have to figure out how to kill all open programs except for this one before it shuts down. I'm going to work on that now.
Also, this might be a very dumb question, but will any of these operations harm my computer in any way? Will it be better if I just learn to do this via powershell?
at /?
. Andshutdown -f -s
will force running programs to quit before shutting down, but-t 30
already implies-f
. \$\endgroup\$at
command the same way you're runningshutdown
, and you wouldn't even need the program to keep running. For something Windows-specific, though, a PowerShell or batch script would still be far simpler and more straightforward. Your friend should be able to run either type of script by double-clicking it. \$\endgroup\$