3

Im developing a desktop app in java that when the app is closed by the frame I have an event call FrameClosing that make a function when user close the windows , the problem is when the app is close by the task manager or when the user turn off the windows and the app is running, i was trying this.

Runtime.getRuntime().addShutdownHook(new Thread() {

    @Override
    public void run() {

       System.out.println("app was stoped");
    }

});

but it's only works if app is closed by the Frame. I was reading about socket my app could can communicate with a website and if the communication between them I could detect that the program was closed but I dont really too much about it I just want to detect when my app stop running

Darwin Rosso
  • 59
  • 1
  • 7

2 Answers2

0

This answer is talking about C++ but the second point is the important part. If the program is killed via Task Manager or kill -9, there's really not much you can do about that. The program doesn't have access to that level and you'd have to have the program be looking for that specifically.

Rather than try to deal with that, have you considered designing the app so that an unexpected shutdown isn't a deal breaker? For example, doing intermediate logging while the program is running. You could use that to determine if the app was prematurely shutdown when its run next and do whatever is necessary before it starts working.

You could also look at this thread. Its talking about killing via the command line but it might help you in some situations.:

Logically, SIGHUP (terminal hangup) should be raised.

The equivalent of SIGHUP is provided through the callback you register with
SetConsoleCtrlHandler. Your callback function will be called on an 
arbitrary threadpool thread with dwCtrlType = CTRL_CLOSE_EVENT. You've got 
5 seconds to clean-up, you cannot cancel the close.
Community
  • 1
  • 1
Daniel
  • 2,435
  • 5
  • 26
  • 40
  • my app send a request to a php page to notify that the user is connected when the app is close send another request to notify that the user is disconnect but if app is no close from the X button of the windows nothing happen – Darwin Rosso Jul 29 '15 at 14:52
  • @DarwinRosso Well there's a simple solution to that: A Timeout. Your app has to send a signal (a "heart beat") to the php page every X seconds. If it does not, the client has been closed. – LorToso Jul 29 '15 at 15:00
  • how can I do that with java and php? – Darwin Rosso Jul 29 '15 at 15:03
  • @Darwin Set up a heartbeat protocol: every so often (say, once a second), send the equivalent of "hey, i'm alive" to the PHP. Once the PHP hasn't seen that message for 10 seconds or so, it automatically starts its shutdown process – Daniel Jul 29 '15 at 15:11
0

Your code is correct. If it is killed via the task manager though that is end of story. It is killed before it can run System.out.println. If that wasn't the case you would be able to do all sorts of stuff after the user said they want to kill it.

Tea Curran
  • 2,923
  • 2
  • 18
  • 22