t e m p o r a l 
 d o o r w a y 

Multitasking Friendly Ways To Use Timers

 

Some of us make use of timers to terminate an operation or a program. A simple way to use a timer is:

   StopFlag = false;
   Timer1->Enabled = true;
   while(StopFlag == false) Application->ProcessMessages();

On a multitasking system it is rude to sit around chewing up resources waiting for something to happen. Other programs want to use the CPU, and you're just slowing them down. For that matter, your program wants to use the CPU, and it's being slowed down.

if all you want is to kill an application after a certain period of time (let's say a demo or an animation), then the application should start the timer and the timer event handler should contain Application->Terminate();

Let's say you are using the timer to cancel some operation which is taking too long.

Someone presses a button to start the operation, where you do:

   StopFlag = FALSE;
   StopTimer->Enable = TRUE;

   while (IDoMyThing())
   {
      Application->ProcessMessages();
      If (StopFlag) break;
   };

And the timer OnTimer event sets the variable StopFlag.

On the other hand, if you are doing something like blinking a sign, all of the logic goes in the timer event handler, and the application basically does nothing except what's in there.

Someone pushes the button:

   Timer->Enable = TRUE;

And the OnTimer event handler might contain something like:

   if (WarningMemo->Font->Color == clRed) WarningMemo->Font->Color = clMaroon;
   else WarningMemo->Font->Color = clRed;

If you're using the timer to kill a splash form, the same way of doing things is what you want. FormShow() contains

   SplashTimer->Enable = TRUE;

and the SplashTimer handler contains:

   SplashForm->Visible = FALSE;

or however you get rid of the splash form.

Copyright © 2004 by Mark Cashman (unless otherwise indicated), All Rights Reserved