Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I capture a program halt / stop event?

Posted on 2005-04-18
3
Medium Priority
?
231 Views
Last Modified: 2010-05-18
Hello,
I wanted to know how to capture a CTRL-C or program stop event, and execute some shutdown code prior to the program quitting. Can anyone tell me how to do this?

Worth 500 points.

Thanks,
Rick
0
Comment
Question by:richardsimnett
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13811643
0
 
LVL 2

Expert Comment

by:limaideal
ID: 13817212
Do someting like this:

public void main(String args[])
{
...
       Runtime.getRuntime().addShutdownHook(new Thread()
       {
            public void run()
            {
                youShutdownCode();
                yourCleanupCode();
                ....
            }
        });

...

}
0
 

Author Comment

by:richardsimnett
ID: 13856228
Ok guys,
Both of your solutions work sorta. Meaning they work from the command prompt... the java app I need this for runs as a service under windows, and does not seem to work when a "net stop" event occurs. Can you guys tell me how to catch this and make sure the shutdown process runs?

Thanks,
Rick
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question