在 Java 中退出计时器

Exiting Timer in Java

在我的 Java 应用程序中,我有一个计时器来跟踪某人没有扫描二维码的时间。 20 秒后,我抛出一条消息告诉他们使用手动输入而不是扫描二维码。但是,计时器启动后如何停止?例如,用户转到 QR 扫描面板。 20 秒计时器启动。用户成功扫描二维码。现在我怎样才能停止计时器?计时器在我的代码中是这样的。

Timer timer = new Timer();

        timer.schedule( new TimerTask(){

        public void run() { 
               //delay = 20000;

               // if the timer has not been stopped, proceed with timer
               if(!SpectroClick.stop_timer){
                   //System.out.println("20 seconds later");

                    //scanQRPanel.getCameraView().getWebcamPanel().pause();
                    stopScanning();

                    // Create the message to display
                    String text = "No QR code has been found. We suggest that you use a Manual Override." + "<br><br>" + "Press 'OK to confirm, or 'Cancel' to continue scanning for another QR code.</br>";
                    String message = "<html><div style=\"text-align: center;\">" + text + "</html>";

                    // Show the confirmation dialog screen, and wait for user input
                    int i = JOptionPane.showConfirmDialog((Component) null, message,
                            "Confirm", JOptionPane.OK_CANCEL_OPTION);

                    // User selected 'OK'
                    if (i == JOptionPane.OK_OPTION) {
                        for (SpectroClickEventListener listener : listeners) {
                            listener.userSelectedOverride();
                        }
                    }                       

                    // User did not select ok, go to new test
                    else{                           
                        startScanning();
                    }
               }
            }
         }, delay);

我有条件检查计时器是否正在使用,但是有没有办法向计时器抛出某种异常?

问题已经回答here

您正在寻找的方法是 "cancel()" 和 "purge()"。

我已将它们放入您的代码中,但还没有机会 运行:

Timer timer = new Timer();

        timer.schedule( new TimerTask(){

        public void run() { 
               //delay = 20000;

               // if the timer has not been stopped, proceed with timer
               if(!SpectroClick.stop_timer){
                   //System.out.println("20 seconds later");

                    //scanQRPanel.getCameraView().getWebcamPanel().pause();
                    stopScanning();

                    // Create the message to display
                    String text = "No QR code has been found. We suggest that you use a Manual Override." + "<br><br>" + "Press 'OK to confirm, or 'Cancel' to continue scanning for another QR code.</br>";
                    String message = "<html><div style=\"text-align: center;\">" + text + "</html>";

                    // Show the confirmation dialog screen, and wait for user input
                    int i = JOptionPane.showConfirmDialog((Component) null, message,
                            "Confirm", JOptionPane.OK_CANCEL_OPTION);

                    // User selected 'OK'
                    if (i == JOptionPane.OK_OPTION) {
                        for (SpectroClickEventListener listener : listeners) {
                            listener.userSelectedOverride();
                        }
                    }                       

                    // User did not select ok, go to new test
                    else{                           
                        startScanning();
                    }
               } else{
                  timer.cancel();
                  timer.purge();
               }
            }
         }, delay);

您不应为此使用 java.util.Timer,而应使用 Swing Timer,因为您当前的方法违反了 Swing 的单线程规则。

一个 Swing Timer 可以配置为单个 运行,这意味着您无需担心自己停止它

查看 Concurrency in Swing and How to use Swing Timers 了解更多详情