Re: Creating dynamic TTimers with Nil owner



On Mon, 22 Aug 2005 19:02:27 +0000 (UTC), "Steve R."
<obscure@xxxxxxxxx> wrote:

>Could someone please provide an example of how I could create and free
>multiple dynamic TTimers at runtime with a *Nil* owner? For example, I
>want to create X-amount of dynamic TTimers (all running at once) in a
>onCreate event handler of a form. The number of TTimers will be determined
>at runtime. I really need to know how to assign event handlers to the
>timers and how to make sure each instance of the timers is freed when the
>timer event handler(s) have halted.

I assume this is just for demo purposes, and that you realise you
could use the form as the owner. That being so:

Start a new project and drop a TMemo and a TCheckbox on the form. Set
the Checked property of the TCheckbox to TRUE. This checkbox will be
used to toggle the timers on & off.

Add this code:

----------------------%<---------------------------------------------

uses
ExtCtrls;

procedure TForm1.FormCreate(Sender: TObject);
const
MIN_TIMER_RESOLUTION = 60;
MAX_TIMER_INTERVAL = 60000; // 1 minute for demo
var
TimersRequired : integer;
iTimer : Integer;
NewTimer : TTimer;
begin
// Set the number of timers to be created
// Max count of 20 for demo purposes
RandSeed := trunc(now);
TimersRequired := Random(20);

// Create a list to hold all our timers so we can be sure to free
// them again when we close. This also allows us to refer to them
// (for example, to turn them on or off) if we need to.
FTimers := TList.Create;

for iTimer := 0 to pred(TimersRequired) do
begin

// Now create the timers and add them to a list
// Add a timer & set properties
NewTimer := TTimer.Create(nil);

// Add the timer to the list to make sure we can find it later.
// Do this BEFORE doing anything else in case of exceptions.
FTimers.Add(NewTimer);

// Now set the timer properties, including the event handler so we
// can react when the timer fires
NewTimer.Name := 'Timer' + IntToStr(FTimers.Count);
NewTimer.Interval := MIN_TIMER_RESOLUTION +
random(MAX_TIMER_INTERVAL - MIN_TIMER_RESOLUTION);

///////////////////////////////////////
// ASSIGN THE EVENT HANDLER
///////////////////////////////////////
NewTimer.OnTimer := NoteTimerFired;

NewTimer.Enabled := TRUE;

end;
end;

procedure TForm1.NoteTimerFired(Sender: TObject);
begin
// This is the OnTimer event handler for all the timers
if Sender is TTimer then
// One of our timers fired.
// Just note which one so we can see something happen
Memo1.Lines.Add(
(Sender as TTimer).Name +
' Fired at ' + TimeToStr(Now) +
' Interval ' + IntToStr((Sender as TTimer).Interval)
);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
// Free all the timers (and remove them from our list)
while FTimers.Count > 0 do
begin
TTimer(FTimers[0]).free;
FTimers.Delete(0);
end;

// Don't forget to free up the list as well
FTimers.free;

end;

procedure TForm1.CheckBox1Click(Sender: TObject);
var
iTimer : integer;
begin
// Toggle the timers on or off
if assigned(FTimers) then
for iTimer := 0 to pred(FTimers.Count) do
TTimer(FTimers[iTimer]).Enabled := CheckBox1.Checked;
end;


----------------------%<---------------------------------------------

--
Duncan



.



Relevant Pages

  • Re: System.Timers.Timer
    ... TIA ... I need a sample with timers. ... and add an event handler to the timer's Tick event. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: .NET Event Handling (timers, Mouse movement)
    ... > mentioned am learning .NET. ... > out how to make an event handler for timers or mouse actions. ... properties window. ...
    (microsoft.public.vc.language)
  • Re: System.Timers.Timer
    ... I need a sample with timers. ... I read the samples on the net with consol output but I want to change (for ... and add an event handler to the timer's Tick event. ...
    (microsoft.public.dotnet.languages.vb)
  • linux-next: tip tree build warnings
    ... Introduced by commit 3f0a525ebf4b8ef041a332bbe4a73aee94bb064b ("timers: Add tracepoints for itimer") from the tip tree. ...
    (Linux-Kernel)
  • Re: multiple timers
    ... after reading several articles about the itimer. ... > SIGPROF), can only be used in a process. ... timer and keep a table of all the timers you're handling. ...
    (comp.unix.programmer)