Reputation: 211
I have, a game bot which runs through console. I don't think i'm going to code a gui for it but i would like to have the possibility to close the program without CTRL+C cause this just interrupts my program instead of properly cleaning up the code and ensure that theres no leaks.
Should i use som kind of Key Bindings or am i bound to, make a GUI ? Or how could i go about this ?
Upvotes: 0
Views: 418
Reputation: 27474
David's answer is good. It may well work for you.
I generally prefer not to deliberately abort a process and then detect that it's coming down to avoid dangling operations. So I'd probably do more like:
Are you processing console inputs as they are entered? IF so, you could just have a console command that tells the app to shut down, and check for this in whatever your process is of handling console inputs. Or if you mean that the app just runs with no user input, you could just periodically check if the console buffer is empty (Console.reader().ready()), and if not, read the console and see if it's the quit command.
Upvotes: 1
Reputation: 862
I think thats what a shutdown hook is for.
Runtime.getRuntime().addShutdownHook( new Thread() {
@Override
public void run() {
System.out.println("Application shutdown");
}
});
Upvotes: 5