简体   繁体   English

如何在SwingWorker线程中取消done()?

[英]How to cancel done() in a SwingWorker thread?

I am making a program which should be able to stop the whole process on the click of a button. 我正在制作一个程序,只要单击一下按钮,它就可以停止整个过程。

I have used sw.cancel(true); 我用过sw.cancel(true); to do so, however, the SwingWorker method protected void done() is still operating. 为此, SwingWorker方法protected void done()仍在运行。

How can I on a push of a button cancel the whole thing? 我如何按一下按钮就可以取消整个过程? Not just the doInBackground() method? 不只是doInBackground()方法?

Here's the whole relevant code below for those who are interested: 以下是有兴趣者的全部相关代码:

sw = new SwingWorker() {
    protected Object doInBackground() throws Exception {
        //Pravljenje timera
        t = new Timer(0, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });

        //Provera stanja checkboxova
        if(jCheckBox2.isSelected()) {
            try {
                int delay =(int) jSpinner2.getValue();
                jCheckBox1.setSelected(false);
                Thread.sleep(delay*60000);
            } catch (InterruptedException ex) {
                Logger.getLogger(App_Gui.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if(jCheckBox1.isSelected()) {
            jCheckBox2.setSelected(false);   

            Date delay2 = (Date) jSpinner1.getValue();
            userCal = Calendar.getInstance();
            System.out.println("delay2: " + delay2);
            userCal.setTime(delay2);
            System.out.println("userCal:" + userCal);
            Calendar sysCal = Calendar.getInstance();
            System.out.println("sysCal: " + sysCal);
            timerAtDate = 
                (((int) userCal.get(Calendar.HOUR_OF_DAY) - (int) sysCal.get(Calendar.HOUR_OF_DAY)) * 60 * 60 * 1000 +
                ((int) userCal.get(Calendar.MINUTE) - (int) sysCal.get(Calendar.MINUTE)) * 60 * 1000 +
                ((int) userCal.get(Calendar.SECOND) - (int) sysCal.get(Calendar.SECOND)) * 1000);
            Thread.sleep(timerAtDate);
        }

        return null; 
    }

    @Override
    protected void done() {
        System.out.println("Done!");

        t.start();
        t.setRepeats(false);
        JDialog dialog = new JDialog();
        dialog.setLocation(700, 300);
        dialog.setSize(600, 400);
        dialog.setVisible(true);
        try {
            Thread.sleep(jSlider1.getValue());
        } catch (InterruptedException ex) {
            Logger.getLogger(App_Gui.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Zapravo gotov");
        dialog.getContentPane().setBackground(jLabel2.getBackground());
        dialog.setModal(true);
        Assignment_Tajmer_Aplikacija.f.setVisible(false); 
    }
};

sw.execute();

private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
    // TODO add your handling cosadde here:
    jSlider1.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            //System.out.println(source.getValue());
        }
    });
}                                     

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    JColorChooser jcc = new JColorChooser();
    Color c = jcc.showDialog(null, "Choose background color", Color.yellow);
    jLabel2.setForeground(c);
    jLabel2.setBackground(c);
    jLabel2.setText("Color: " + c.getRGB() + "(RGB)");
}                                        

private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jCheckBox1.setSelected(false);
}                                          

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    sw.cancel(true);
}                                        

private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jCheckBox2.setSelected(false);
}                                          

使用isCancelled()方法的开始done() ,看看你应该做的操作与否。

You can't cancel it but you can short-circuit it so that it finishes early. 您不能取消它,但是可以使其短路以使其尽早完成。 Use some shared state flag between your threads to signal. 在线程之间使用一些共享状态标志来发出信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM