Close Application when Exit

I have a begginer problem that when I click the X button on the jME window the window closes but the process remains open. Does that mean that I am leaving some threads running and need to be stopped? any quick hack to close the remaining threads (if that’s the case)?

thanks

Step 1: figure out what threads you have running.
Step 2: set them up as daemon threads or properly close/manage them through an app state or on app close.

thanks! and is there a way to catch that event? like I can catch the ESC event and call say System.exit(0) - of course that’s a horrible way to close the window but just curious how I can catch the event for the window close when the user clicks the X

Well, @nehon says this may be broken for “x”. For thread management I rely on AppState.cleanup() which is called during shutdown. But it may be that this isn’t working when the ‘x’ is clicked. I’m not in a position to test right this minute.

Actually if you have a poolexecutor in the Application you need to shut it down properly when closing the application. The PROPER way of doing it is to override the app.destroy() and to shut down your poolExecutor there. Don’t forget to call super.destroy()

The “proper” way would be to manage your pool with an app state and shut it down during app state cleanup(). :slight_smile: Or any other non-daemon threads for that matter.

Next to that, you can go with the more fragile “override app.destroy() and make sure to call super.destroy() or face your peril”. But an app state would be better.

nehon has confirmed that the code paths are hooked up so you can ignore my earlier post suggesting that they might not work.

nehon has confirmed that the code paths are hooked up so you can ignore my earlier post suggesting that they might not work.

so how can I catch the ‘x’ event??

and if I write something like this in the main thread

[java]
@Override
public void destroy(){
this.stop();
super.destroy();
}
[/java]

what else I should add to kill all the other threads running? I am not sure how I can manage the poolExecutor (sorry again from being a noob here)

@nightwolf911 said: so how can I catch the 'x' event??

and if I write something like this in the main thread

[java]
@Override
public void destroy(){
this.stop();
super.destroy();
}
[/java]

what else I should add to kill all the other threads running? I am not sure how I can manage the poolExecutor (sorry again from being a noob here)

Do not call this.stop() yourself. destroy() is called as a result of stop.

Do you have your own threads or not? If you do then you have to manage them. If you don’t then something else is wrong.

yeah I have multiple threads, and I set them all to null when exiting… not sure what’s the problem.

@nightwolf911 said: yeah I have multiple threads, and I set them all to null when exiting.... not sure what's the problem.

a) setting a thread to null is not the same as stopping the thread.
b) that you think that probably means you need to learn more about threads before using them… threading is hard.

Note: calling thread.stop() is not the answer either. The only way to safely stop a thread (externally) in Java is System.exit(). :wink: The proper way is to get them to commit suicide… ie: shut themselves down by returning from run().

If you are using thread pools/executors then it will do most of the magic for you and you just need to call shutdown() on the executor. If you are unfamiliar with threading then you should probably use a single thread executor instead of trying to manage a thread yourself. Though even then you have to design the runnable properly.

Since I don’t know what your thread is doing, I can’t comment further.

You set them to null or you close them?

okay thanks! I was actually using interrupt() since System.exit() was too aggressive and called my external GUI thread.

but still no one mentioned how I can catch the event when the user clicks on ‘x’ , is that even possible?

Well… in a sense, it was answered.

[java]
@Override
public void destroy() { }
[/java]

This will ensure that the code is executed whether shut down by X, or an uncaught exception, etc, etc. Since I am not sure how you are handling your threads, I can only make vague suggestions. However… if they are persist threads utilizing the run() method in the following way:

[java]
@Override
public void run() {
while (someBoolean) {
// Your code
}
}
[/java]

You may want to set the boolean to false on your way out to terminate the persistent thread.

EDIT: Also wanted to mention, that @pspeed had a much better solution all the way around when hinting at the use of a ThreadPoolExecutor

1 Like

…and I will repeat but even clearer, that the better way to “capture the ‘x’ event” is to have an app state that you register during app startup. It’s cleanup method will be called when the app is shutdown regardless of how it was shutdown. If you are trying to prevent the app from shutting down then they may require overriding a different method… I don’t remember for sure.

If you are specifically trying to catch just the user clicking on ‘x’ and nothing else then you may be out of luck. You might have to explain why you want to do it, though.

Oh… you may also want to look into using an AppState, as I hear that the cleanup method is called when the application terminates…

Wait for it…

Wait for it…

Waaaaaaiiiiit for it…

=)

2 Likes
@t0neg0d said: Oh... you may also want to look into using an AppState, as I hear that the cleanup method is called when the application terminates...

Wait for it…

Wait for it…

Waaaaaaiiiiit for it…

=)

Heheh. To be fair, this time I wasn’t picking on you. Just being more explicit about the connection between clicking “x” and having cleanup() called.

There’s also something about overriding a method that you must call super.theSameMethod on that makes me uncomfortable to give as general advice. Especially when there are friendlier alternatives.

2 Likes