C++ComponentsLanguage FeatureLearn C++

Learn to Use Timer Component in C++ Builder on Windows

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;

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,

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,

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!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

Worth reading...
Learn To Use For Loops In C++