C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, macOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition for students, beginners, and startups.
You can download the free C++ Builder Community Edition here: https://www.embarcadero.com/products/cbuilder/starter.
Professional developers can use the Professional, Architect, or Enterprise versions of C++ Builder. Please visit https://www.embarcadero.com/products/cbuilder.
Timer Component
Normally we can use for(), while(), do{}while() functions do do looping functions. We can also use the goto statement and labeling to do loops. All these operations directly run without having any idle time or break for checking UI elements. Loops are faster but sometimes you may need to check or update some elements, functions, or doing calculations, checking the database in intervals. C++ Builder has a great component for VCL and FMX framework applications called TTimer Component used for loops.
Timer Component (TTimer) encapsulates the Windows API timer functions, it is used to simplify calling the Windows API timer functions SetTimer and KillTimer, and to simplify processing the WM_TIMER messages. Use one timer component for each timer in the application.
The execution of the timer occurs through its OnTimer event. TTimer has an Interval property that determines how often the timer’s OnTimer event occurs. The interval corresponds to the parameter for the Windows API SetTimer function.
OnTimer() event occurs when a specified amount of time, determined by the Interval property, has passed.
Write an OnTimer event handler to execute an action at regular intervals. The Interval property of a timer determines how frequently the OnTimer event occurs. Each time the specified interval passes, the OnTimer event occurs.
For example, we can have a TLabel on our form and we can count and print out this to a label. To do this, double click to TTimer Component to create OnTimer() event and write lines as below;
1 2 3 4 5 6 7 |
int count=0; void __fastcall TForm1::Timer1Timer(TObject *Sender) { count++; Label1->Text = IntToStr(count); } |
Normally, Timer Interval is set to 1000 ms, which means 1 second. So, when we run this code above it will count seconds, to speed up we must decrease to lower numbers like 50 100 intervals. When we use timer events it allows us to interact with other UI elements.
The Timer Component is mostly used on updating screen view, i.e. game applications. 32-40 ms Interval is good for game applications, if the hardware GPU is great it can be as low as possible.
Enabling and Disabling Timer
We can easily enable or disable the timer by changing the Enabled property of the Timer component, you can also check or uncheck this event from the object property. For example, by a Button, we want to disable Timer if it is enabled and if it is disabled we want to enable it again when this button is pressed. Simply we can do this,
1 2 3 4 5 6 7 |
void __fastcall TForm1::Button1Click(TObject *Sender) { if(Timer1->Enabled) Timer1->Enabled=false; else Timer1->Enabled=true; } |
Sometimes, operations in this OnTimer() event may take much time, for example, you may have a 15ms Interval, and statements inside this event may take more than 15ms. Which may run OnTimer() event while the previous operations are not finished. To avoid conflict of Timer run, we should disable the OnTimer(), after operation statements we should enable it. See the example below,
1 2 3 4 5 6 7 8 9 |
int count=0; void __fastcall TForm1::Timer1Timer(TObject *Sender) { Timer1->Enabled = false; count++; Label1->Text = IntToStr(count); Timer1->Enabled = true; } |
So the next timer will only run if the current timer operations are done.
We can also run parallel algorithms inside this OnTimer event, so in each event, we can run multiple tasks at the same time.
In conclusion, Timer() is a great loop too in intervals that allow you to control other UI elements while you are doing your looping elements. It is also good to reduce CPU usage which means your applications may consume less energy and this is very important for mobile applications.
Get started building powerful apps with C++Builder!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition