Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Always execute code

 
Mamu Jan
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use Runtime.addShutdownHook. I think that is executed whenever the JVM is terminated, whatever the reason.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mamu Jan
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Mamu Jan
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic