简体   繁体   English

Java:Swing Timer等待

[英]Java: Swing Timer waiting

I have such a code: 我有这样的代码:

final SwingWorker worker = new SwingWorker() { 
      @Override
      protected Object doInBackground() throws Exception {...};
}
Timer timer=new Timer(10000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            worker.execute(); 
        }
    });
    timer.start();

Sometimes worker procedure takes more than 10 seconds, but sometimes 3-4sec, so how to set the timer to wait for the full execution of worker ? 有时工作程序需要10秒以上,但有时需要3-4秒,那么如何设置计时器等待工作人员的完全执行? Any suggestions? 有什么建议?

See the javadoc of the SwingWorker class: 查看SwingWorker类的javadoc:

Before the doInBackground method is invoked on a worker thread, SwingWorker notifies any PropertyChangeListeners about the state property change to StateValue.STARTED. 在工作线程上调用doInBackground方法之前,SwingWorker会将状态属性更改通知给StateValue.STARTED。 After the doInBackground method is finished the done method is executed. doInBackground方法完成后,执行done方法。 Then SwingWorker notifies any PropertyChangeListeners about the state property change to StateValue.DONE. 然后,SwingWorker会将任何PropertyChangeListeners状态属性更改通知给StateValue.DONE。

So events are fired when the SwingWorker is done. 因此,当SwingWorker完成时会触发事件。 Attach a listener and re-activate your Timer on the correct event. 附加一个监听器并在正确的事件上重新激活Timer

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

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