How to stop SwingWorker.doInBackground process immediately?

杀马特。学长 韩版系。学妹 提交于 2019-12-25 06:09:09

问题


According to Oracle docs SwingWorker.doInBackground does not include any code that might throw InterruptedException". Dunno if I'm right about what I understand, but I noticed that cancelling the doInBackground process prints out the InterruptedException so it'll continue even if an Exception is encountered? Then how can we cancel and stop the process immediately?

So I tried return STATUS; when it is cancelled but still continues. I also tried PROCESS.destroyForcibly() before return but still the same. How to stop it immediately once cancelled?

import ...

public class Test {

    private BackgroundProcess BG_PROCESS;
    private JFileChooser FILE_CHOOSER;
    private JProgressBar PROGRESS_BAR;


    public Test() {
        initComponents();
    }

    ...

    private class BackgroundProcess extends SwingWorker<Integer, String> {

        private int STATUS;

        @Override
        protected Integer doInBackground() throws Exception {

            File[] SELECTED_FILES = FILE_CHOOSER.getSelectedFiles();

            for (int i = 0; i < SELECTED_FILES.length; i++) {

                String file = SELECTED_FILES[i].getAbsolutePath();
                String name = SELECTED_FILES[i].getName();

                String[] command = {"javac", "" +file+ ""};

                try {
                    ProcessBuilder PROCESS_BUILDER = new ProcessBuilder(command);
                    Process PROCESS = PROCESS_BUILDER.start();
                    if (!isCancelled()) {
                        STATUS = PROCESS.waitFor();
                        if (STATUS == 0) {
                            TEXTAREA.append(name+ "Successfully compiled.");
                        } else {
                            TEXTAREA.append(name+ "Failed. (not compiled)");
                        }
                    } else {
                        TEXTAREA.append("Compilation cancelled.");
                        return STATUS;
                    }
                    PROCESS.destroy();
                } catch (IOException | InterruptedException ex) {
                    ex.printStackTrace(System.err);
                }
            }

            return STATUS;
        }
    }

    public static void main(String[] args) {
        new Test();
    }
}

Start BackgroundProcess

JButton START = new JButton("Start");
START.addActionListener(new ActionListener() {
    @Override
    ...

    BG_PROCESS = new BackgroundProcess();
    BG_PROCESS.execute();
    PROGRESS_BAR.setIndeterminate(true);
});

Cancel BackgroundProcess

JButton CANCEL = new JButton("Cancel");
CANCEL.addActionListener(new ActionListener() {
    @Override
    ...

    BG_PROCESS.cancel(true);
    PROGRESS_BAR.setIndeterminate(false);
});

来源:https://stackoverflow.com/questions/41559851/how-to-stop-swingworker-doinbackground-process-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!