This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Catching Ctrl C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Catching Ctrl C" Watch "Catching Ctrl C" New topic
Author

Catching Ctrl C

Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
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.......


The strength of the Wolf is the pack & the strength of the pack is the wolf....Rudyard Kipling
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

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

Joined: Apr 14, 2003
Posts: 53
And what happens if someone pulls the plug on the client computer. How do you clean up then?
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
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"


Hope This Helps
Nilesh Pereira
Ranch Hand

Joined: Apr 14, 2003
Posts: 53
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

Joined: Dec 08, 2001
Posts: 159
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Catching Ctrl C
 
Similar Threads
why can't close the remote data?
Closing Java Frame
Catch application closed in console
Handling an Ctrl-C in an Servlet
Catching up ctrl-c