Solved

How To Increase The Timer Interval After The Timer Is Already Started?

Posted on 2010-11-09
4
714 Views
Last Modified: 2012-05-10
I have a data aquisition app that has a procedure that is continuously called by a timer every 3 seconds. Depending on the procedures execution, additional time is sometimes required for the procedure to complete (because of external variables) before its time for the timer to fire and execute it again. I could solve the problem by resyncronizing the timer as the procedure time requirement changes.

Is there a way to extend, or change, the timer interval once the timer's been enabled?
0
Comment
Question by:jsmitter
4 Comments
 
LVL 13

Accepted Solution

by:
rfwoolf earned 250 total points
ID: 34098366
What you should be doing is, on the timer's onTimer event, you disable the timer, and then in a try...finally statement you re-enable it again, e.g.

Timer1.Enabled := false;
try
  //Do your processing
finally
  Timer1.Enabled := true;
end;
0
 
LVL 14

Expert Comment

by:systan
ID: 34099019
you could use the; gettickcount() function
eq.,
var
tick: cardinal;
...
begin
tick := gettickcount();
..
..
tick := gettickcount - tick;
...
if tick>=3000 then
tick := tick - 3000;
...
showmessage(inttostr(tick));
...
0
 
LVL 25

Expert Comment

by:epasquier
ID: 34101144
rfwoolf is almost right. The solution is to stop and restart the timer after setting a different interval. But not on the timer onTimer event, right when your application detects a condition that needs extra time for your process.
I often use a procedure like this one to reset timers. Most of the time, I don't even change the interval, because I just need the same time allowed but from now on. Ex : when recieving data from a COM port, a time-out is usually not from the start of the message, but from the last character received.
procedure ResetTimer(aTimer:TTimer;RemainingTime:Cardinal=0);
begin
 With aTimer do
  begin
   enabled:=False;
   if RemainingTime>0 Then Interval:=RemainingTime;
   enabled:=True;
  end;
end;

Open in new window

0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 250 total points
ID: 34117513
You dont need to change the timer interval at all. Using the OnTimer event is a good solution since you have no way of knowing how long the procedure your are executing will take. Disable the timer until you are done with the process as rtwolf points out.

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  try
    ProcessData;
  finally
    Timer1.Enabled := True;
  end;
end;

The down side of this is that, while the procedure is running, your application has to wait for it to finish and you may be receciving more data to process at the same time.
An even better solution would be to fire a thread and let it handle the incoming data. This way your application is always responsive and you ont have to worry about timers and how long the procedure will take.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question