How could I synchronise the timer with some process that runs during X seconds. I need the timer to count seconds only if the process is running. If the process is terminated, then the timer must stop. Most important is that the process execution time is not known apriori. Otherwise, I could use ScheduledExecutorService.
My code is shown below. The problem is that the program stops responding for X seconds. How could I solve this synchronisation problem? Thanks a lot!
elapsedTimeMin = 0;
elapsedTimeSec = 0;
while (myProcess.isExecuting()) {
timer = new javax.swing.Timer(1000,
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (elapsedTimeSec < 60) {
elapsedTimeSec++;
} else {
elapsedTimeSec = 0;
elapsedTimeMin++;
}
jTextField.setText(elapsedTimeMin + ": " + elapsedTimeSec);
}
});
timer.setInitialDelay(0);
timer.start();
}