Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-11-09
4
Medium Priority
?
739 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 1000 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:Ephraim Wangoya
Ephraim Wangoya earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

804 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