Set the timer to pause

How to pause TTimer in Delphi with storage interval? So, for example, I have TTimer , which has a 10 second interval , and when I set the timer to pause after the first 7 seconds of operation, it will retain its state and then when I resume the timer, it will be fire after the remaining 3 seconds .

Thanks a lot guys!

+6
source share
4 answers

You cannot do this with TTimer , which is a free SetTimer API SetTimer .

To do this, you will need to track when the timer starts and when you pause it. Then you will find out how much time is left. When you need to pause, set the Enabled timer to False and set the interval as the amount of time remaining. Do not forget that after the timer is triggered for the first time, you need to reset its interval to the true interval.

As can be seen from the above, TTimer not suitable for your problem. But, having said that it would not be easy and rather funny to create a variant of TTimer that would maintain a pause as you wish.

+9
source
 Timer.interval :=1000; ICount:integer; 

In the created procedure, set the value of icount to 0

 Procedure timerevent(sender:tobject); Begin If icount=10 then Begin // do proccess Icount = 0; End; Inc(icount); End; 

Now you can stop the timer anywhere

+13
source

This is not how the Windows Timer tool works, nor how the Delphi shell works.

When you turn off the timer, pay attention to how much time is left before it was called again. Before re-enabling it, set the Interval property to this value. The next time the timer starts, reset Interval to its original value.

+2
source

TTimer does not support what you are asking for. As other users have already commented, you will need to stop the timer, reset its Interval to any remaining time, start it, and then stop and reset its Interval to return to 10 seconds when the next OnTimer event is fired.

A simpler solution is to simply let TTimer work fine and have a separate flag that tells the OnTimer event handler whether it can do its job or not when it starts, for example:

 var Boolean: Paused = False; procedure TForm1.Timer1Timer(Sender: TObject); begin if not Paused then begin // do proccess end; end; procedure TForm1.PauseButtonClick(Sender: TObject); begin Paused := True; end; procedure TForm1.ResumeButtonClick(Sender: TObject); begin Paused := False; end; 
+1
source

Source: https://habr.com/ru/post/903619/


All Articles