Solved

How do I suspend a timer ?

Posted on 1999-01-20
9
258 Views
Last Modified: 2010-04-06
Is there any way to temporarily suspend a timer ? I'm writing a small automatic file back-up program (hopefully to run as a Tray Icon application) and would like to suspend the timer while the user is changing their back-up options (to prevent a back up going off while the user is changing things). At first I thought to set the interval property to 0 while the user is changing their options, but if I reset interval to it's original value afterwards then the timer starts from scratch again. Is there some way round this or am I barking up the wrong tree by trying to use the timer for this ?

Thanks


Matt Freake
0
Comment
Question by:paninaro
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1362479
You could set enabled to false, then set it to true again to set it going again. Though I am not sure if this will restart the timer again from scratch. If this works dibs on the answer!

Cheers,

Raymond.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1362480
Hi paninaro,

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if YourGlobalVarInEdit then
    DoNothing
  else
    otherStuff;
end;

YourGlobalVarInEdit typ Boolean initialized False
By Begin of Usermanipulatuin set to true
on End to False

Other Solution is to disable the Timer if true
instead

donothing

Timer1.Enabled := False;

must be enabled after Usermanipiulation

meikl
0
 
LVL 20

Accepted Solution

by:
Madshi earned 200 total points
ID: 1362481
Hi Matt,

I've written a little component for you...
Go to "component -> install component" and install this unit.
You can use the "Suspended" property.

unit exttimer;

interface

uses extCtrls;

type
  TExtTimer = class (TTimer)
  private
    FSuspended : boolean;
    FFireEvent : boolean;
    procedure SetSuspended (value: boolean);
  protected
    procedure Timer; override;
  published
    property Suspended : boolean read FSuspended write SetSuspended;
  end;

procedure Register;

implementation

uses classes;

procedure TExtTimer.Timer;
begin
  if FSuspended then begin
    enabled:=false;
    FFireEvent:=true;
  end else inherited;
end;

procedure TExtTimer.SetSuspended(value: boolean);
begin
  if value<>FSuspended then begin
    FSuspended:=value;
    if FFireEvent then begin
      FFireEvent:=false;
      inherited Timer;
      enabled:=true;
    end;
  end;
end;

procedure Register;
begin
  RegisterComponents('Madshi', [TExtTimer]);
end;

end.

Regards, Madshi.

P.S.: Disabling/Enabling the timer starts it from the scratch...
0
Industry Leaders: 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!

 
LVL 12

Expert Comment

by:rwilson032697
ID: 1362482
Madshi: Doesn't your solution effectively just toggle the enabled property?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1362483
Raymond,

nope... That would be a joke, wouldn't it???

Let's say we have a timer that fires every 10 seconds. Then you suspend the timer after the 12 second for 5 seconds, then the suspend has no effect in my timer routine. The next event fires after 10, 20, 30... seconds. If you would use the enabled property, the next event would fire after 10, 27, 37... seconds!
If the timer is suspended after the 17 second for 5 seconds, then the 20 second timer doesn't fire. But my component remembers that there's an event delay. So in the moment when you resume the timer, the event is fired at once. That means my component would fire at 10, 22, 32, 42... If you would use the enabled property, the timer would fire at 10, 32, 42...

You see the difference?

Matt,

please look at the examples I've given in this comment. Does my component react the way you want it to?
0
 

Author Comment

by:paninaro
ID: 1362484
Thanks, I've just installed the component and it works fine. I have to admit to not thinking it all through properly in the first place and following a discussion with a colleague have realised that, in an ideal world, the component would reset itself to it's old pattern once it had dealt with an abnormal event (in your last example it would fire at 10, 22, 30, 40...). Is that an easy modification to your component ?

Either way, the points are yours, let me know.

Thanks


Matt
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1362485
Hmmm. What if you suspend the timer at 17 for 15 seconds?
Do you want to have 10,32,32,40,50,... or 10,32,40,50,... ?
The first case is not so nice. In the second case one timer event would be lost...

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1362486
Hmm. If you still want me to modify the component, you've got to let me know how exactly (see my last comment). If you're satisfied with the current solution: fine...   :-)

Regards, Madshi-
0
 

Author Comment

by:paninaro
ID: 1362487
That's OK, I've actually decided to write this particular application slightly differently. However I'm sure the timer will come in very useful with some other stuff I'm doing soon.

Many thanks

Matt
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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