wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Always execute code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Always execute code" Watch "Always execute code" New topic
Author

Always execute code

Mamu Jan
Ranch Hand

Joined: Sep 21, 2005
Posts: 45
Hi,

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: 42277
    
  64
You could use Runtime.addShutdownHook. I think that is executed whenever the JVM is terminated, whatever the reason.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Mamu Jan:
Hi,

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?


Tony Morris
Java Q&A (FAQ, Trivia)
Mamu Jan
Ranch Hand

Joined: Sep 21, 2005
Posts: 45
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
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
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 ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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
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.


Silly me !! Thanx 4 all da help Paul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Always execute code