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();
}

I've tried the code shown below. However, I must apriori know the process execution time, isn't it? For example, this code runs 1 hour: ( ... timerHandle.cancel(true); } }, 60 * 60, TimeUnit.SECONDS);} ... )
How could I use this code if I don't know apriori the process execution time (it is random value)? The timer must stop counting, if myProcess.isExecuting()returns false. Please, guru, help me!!! Thanks!

private class TimerControl {
		private int elapsedTimeMin = 0;
		private int	elapsedTimeSec = 0;
		
		private final ScheduledExecutorService scheduler = 
		       Executors.newScheduledThreadPool(1);

		public void runTimer() {
		        final Runnable MyTimer = new Runnable() {
		                public void run() { 
								  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();
		                }
		            };
		        final ScheduledFuture<?> timerHandle = 
		            scheduler.scheduleAtFixedRate(MyTimer, 0, 10, TimeUnit.SECONDS);
		        scheduler.schedule(new Runnable() {
		                public void run() { timerHandle.cancel(true); }
		            }, 60 * 60, TimeUnit.SECONDS);
		}
	}

I suppose I've solved the problem:

final ScheduledFuture<?> timerHandle = 
		            scheduler.scheduleAtFixedRate(MyTimer, 0, 10, TimeUnit.SECONDS);
		        scheduler.schedule(new Runnable() {
		                public void run() { 
		                	while (myProcess.isExecuting()) {
		                	   timerHandle.cancel(true); 
		                	} timer.stop();
		                } 		                
		            }, 0, TimeUnit.SECONDS); 
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.