• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catching Ctrl C

 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a Java GUI Application which has a Db connection.
Now when the application window is closed using the normal 'X' button on the top right corner,I can catch the WindowClosing event and close the db connection.
But since the application is running from a dos prompt,I can even close it using a Ctrl C.
Is there any way for me to catch the Ctrl C and the take appropriate action like closing the open db connections?
Thanks in advance,
Chinmay.......
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime.addShutdownHook(Thread hook)
This won't keep the program from terminating, but lets you do some cleanup while it does.
 
Nilesh Pereira
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happens if someone pulls the plug on the client computer. How do you clean up then?
 
Rodney Woodruff
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't clean up. But you could put some start up code that would try to fix what was not properly released during the last running of the application. This could be rebuilding logs or reporting on last state of the application.
However, outside of using a UPS, there is no such thing as a "Loss of Power Exception or Event"
 
Nilesh Pereira
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My point exactly. Which is why I don't think it's a good practice to trap Ctrl-C. What if the user does an "end task" in Windows, or "kill -9" in UNIX... or the network dies... or a hard disk crashes. There are just too many possibilities. It makes better sense to do damage control either on the server side, or while starting up as Rodney ponted out.
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Point accepted Nilesh!
But when u r working on a single db connection,u have to try and plug as many ambuities as u can.
Thanks,
Chinmay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic