Solved

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

Posted on 2010-11-09
4
724 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Ephraim Wangoya
Ephraim Wangoya 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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 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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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