This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I need to execute some code even if my GUI application is terminated abnormally for e.g. by ending the respective process from the CTRL+ALT+DEL list. I tried putting the code in the windowClosed( WindowEvent e ) function but the code is never executed.
Thanx
There is no patch for ignorance...
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35247
7
posted
0
You could use Runtime.addShutdownHook. I think that is executed whenever the JVM is terminated, whatever the reason.
Correct. Shutdown hooks are always run, unless you hard-kill the JVM (kill -9 under Linux, the Processes tab of the Windows task manager). Even closing the JVM using the Applications tab does not circumvent shutdown hooks.
I need to execute some code even if my GUI application is terminated abnormally for e.g. by ending the respective process from the CTRL+ALT+DEL list. I tried putting the code in the windowClosed( WindowEvent e ) function but the code is never executed.
Thanx
Sounds like you're trying to work around a deeper underlying flaw. Why is your application ever in an "inconsistent" state? I'd investigate attempting to prevent such a state, rather than (erroneously) forcing some arbitrary code to restore consistent state. I could, after all, pull the plug out of the wall - am I doomed? I've used software before where this is the case - are you adding another example?
Originally posted by Ulf Dittmer: You could use Runtime.addShutdownHook. I think that is executed whenever the JVM is terminated, whatever the reason.
I tried it out but it executes the code even before I press CTRL^C or terminate the app frm the process list. It should be executed ONLY when the app is being terminated. Any idea's??
Thanx Ulf
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
Originally posted by Mamu Jan:
I tried it out but it executes the code even before I press CTRL^C or terminate the app frm the process list. It should be executed ONLY when the app is being terminated. Any idea's??
Huh? What is triggering the execution of your shutdown hook, then? That sounds unexpected!
There is no emoticon for what I am feeling!
Mamu Jan
Ranch Hand
Joined: Sep 21, 2005
Posts: 45
posted
0
I have no idea. Here's my code:
That was the thread class. Here's the main app
It prints "Printing" for some time and then suddenly the thread is executed even before I press CTRL^C
Any idea's?? [ November 21, 2005: Message edited by: Mamu Jan ]
Well, yeah. When you create a ShutdownThread its constructor gets called. And in that constructor you call the thread's "start" method. Once you see that you should not be surprised that the thread starts as soon as you create it.
Mamu Jan
Ranch Hand
Joined: Sep 21, 2005
Posts: 45
posted
0
Originally posted by Paul Clapham: Well, yeah. When you create a ShutdownThread its constructor gets called. And in that constructor you call the thread's "start" method. Once you see that you should not be surprised that the thread starts as soon as you create it.