If Java application is closed but not close to frame

I am developing a desktop application in java which, when the application is closed by a frame, I have a call to FrameClosing that does a function when the user closes windows, the problem is that the application is closed by the task manager, or when the user shuts down windows and the application is running, I tried this is.

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

    @Override
    public void run() {

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

});

      

but it only works if the app is frame-closed. I read about socket, my application could communicate with the website and if there was communication between them I could detect that the program was closed, but I don't have too much about it I just want to detect when the application stops working

+3


source to share


2 answers


This answer is talking about C ++ , but the second point is the important part. If the program was killed using the task manager or kill -9

, in fact, you cannot do this. The program does not have access to this level, and you will have to look for the program in this one.

Rather than trying to deal with it, have you considered designing your application so that the unexpected termination is not a violation of the transaction? For example, intermediate logging is performed while the program is running. You can use this to determine if an application has been prematurely disabled when it is restarted, and do whatever is necessary before it starts running.



You can also watch this thread . It talks about killing via the command line, but may 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.

      

0


source


Your code is correct. If killed through the task manager, that's the end of the story. He is killed before he can run System.out.println. If it wasn't, you could do all sorts of things after the user said they wanted to kill him.



0


source







All Articles