Solved

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

Posted on 2010-11-09
4
708 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now