开发者

No exit option if something is running

开发者 https://www.devze.com 2023-02-03 21:21 出处:网络
How can I catch something going on if the user chooses the exit option from a menu? I mean I\'d like to be able to manage the event of a user who is going to close the application

How can I catch something going on if the user chooses the exit option from a menu? I mean I'd like to be able to manage the event of a user who is going to close the application but some activity is being performed so he/she shouldn't be able to exit.

Here's the code I wrote. In a nutshell :

  • recording is being performed --> user clicks exit --> WARNING "Process is running you can't do it"(The process goes on)

  • noth开发者_JAVA技巧ing is running --> user clicks exit --> application closes

Is it possible to solve the problem by just adding a few lines of code without having to rewrite the entire program?

thanks very much in advance.

MAX

exitAction = new AbstractAction("Exit") {

            public void actionPerformed(ActionEvent e) {

                System.exit(0);
            }
        };
        exitAction.putValue(Action.NAME, "Exit");
        // description 
        exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit application");


You need to keep track of your jobs in order to do this cleanly. If you can't you could try to iterate to all the jvm threads and looking to see if some of your worker threads are in a executable state or not.

Remember that the user cand kill the application with the big Task Manager axe if he so wishes :).


You could use a variable, a little flag, you set it to true at the begining of the process you wanna check, whe it ends set it to false.

Then in the closing event you can check if that variable is true or false.


If you are not using threads in your application (I am assuming you are, but am covering it for clarity's sake), then, when you exit out of your application, the Event Dispatch Thread (EDT) will close down everything for you.

Otherwise, it is important that you correctly manage and track your threads in the system so that you can go and end them before you shut down the system. There are a number of ways to do this (PLEASE DO NOT USE stop() and try to avoid destroy()). My recommendation is that, within your threads, take the time to check to see if they should shut down (a flag that is set by the system upon shutdown is one such possibility). That way, you can clean up your threads, save any data (to a file or whatever, if that is what you need to do), and then cleanly shut down.


If by 'running' you mean unfinished threads, then you can loop all threads you have started and call .join(). before that display a message 'waiting for all threads to finish. Do you want to force close'

0

精彩评论

暂无评论...
验证码 换一张
取 消