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

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn to Use Timer Component in C++ Builder on Windows
By Yilmaz Yoru May 29, 2021

Для просмотра ссылки Войди или Зарегистрируйся 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: Для просмотра ссылки Войди или Зарегистрируйся.

Professional developers can use the Professional, Architect, or Enterprise versions of C++ Builder. Please visit Для просмотра ссылки Войди или Зарегистрируйся.

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 (Для просмотра ссылки Войди или Зарегистрируйся 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.

Для просмотра ссылки Войди или Зарегистрируйся event occurs when a specified amount of time, determined by the Для просмотра ссылки Войди или Зарегистрируйся property, has passed.

Write an OnTimer event handler to execute an action at regular intervals. The Для просмотра ссылки Войди или Зарегистрируйся 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;
C++:
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,
C++:
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,
C++:
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.