Re: Creating dynamic TTimers with Nil owner
- From: Duncan McNiven <spamtrap@xxxxxxxxxxxxxxxxxx>
- Date: Sat, 27 Aug 2005 18:00:32 +0300
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
.
- Prev by Date: A big thank you to all on here
- Next by Date: posting 20050827
- Previous by thread: A big thank you to all on here
- Next by thread: posting 20050827
- Index(es):
Relevant Pages
|