In C++ Builder, the global variable Application (TApplication), is in every VCL-based application. Application encapsulates your application as well as provides many functions that occur in the background of the program. For instance, the Application handles how you call a Help file from the menu of your program. Understanding how TApplication works is more important to a component writer than to developers of stand-alone applications, but you should set the options that Application handles in the Project-> Options Application page when you create a project.
In addition, Application receives many events that apply to the application as a whole. For example, the OnActivate event lets you perform actions when the application first starts up, the OnIdle event lets you perform background processes when the application is not busy, the OnMessage event lets you intercept Windows messages (on Windows only), the OnEvent event lets you intercept events, and so on. Although you can’t use the IDE to examine the properties and events of the global Application variable, another component, TApplicationEvents, intercepts the events and lets you supply event-handlers using the IDE.
In this post, you’ll learn how to define and use the idle loop in C++ Builder on Windows, how to set a method to the Application’s OnIdle method, what OnIdle is, and how to detect if my application is currently idle. In C++ IDE, we can create a new Idle Loop. So our application can execute processes in its IdleTime. We can perform effects and animations. We can also use this to handle games, simulations, or other graphical events. Let’s see how we can create our idle loop in C++.
Table of Contents
What is the OnIdle event?
OnIdle event (Vcl::Forms::TApplication::OnIdle) is an event method of the Application. TApplication that occurs when an application becomes idle. We can code an OnIdle event handler to perform special processing when an application is idle. An application is idle when it is not processing code. For example, an application is idle when it is waiting for input from the user.
Note that OnIdle is called only once, as the application transitions into an idle state. It is not called continuously unless the Done
parameter is set to false
. Applications that set Done
to false
consume an inordinate amount of CPU time, which affects overall system performance.
Note: You can also respond to this event using the TApplicationEvents component, which allows you to assign an event handler using the IDE.
What is Syntax of the OnIdle event?
Here is the syntax of the OnIdle event,
1 2 3 |
__property TIdleEvent OnIdle = {read=FOnIdle, write=FOnIdle}; |
we can declare new OnIdle Idle Loop method. Here is a syntax for the IdleLoop example below,
1 2 3 |
void __fastcall IdleLoop(TObject*, bool& done); |
We can define IdleLoop this method as below,
1 2 3 4 5 6 7 8 |
void __fastcall TForm1::IdleLoop(TObject*, bool& done) { done = false; // Do OnIdle events here } |
Finally we can set this IdleLoop method to OnIdle property of our Application as below,
1 2 3 |
Application->OnIdle = IdleLoop; |
Here is an example of a header file which uses an IdleLoop
On the header of our form, we just need to add a public integer variable count and declaration of IdleLoop() as below. In this example, we have Memo (TMemo), Label (TLabel) and we have Button (TButton) with ButtonClick event. Add these components to follow the example. In this example we want to count and print to Label1 when the application is idling. Also, we will show this count when the user clicks on the button – at which point we will reset the count. Here is the header file,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//--------------------------------------------------------------------------- #ifndef Idle_Loop_Unit1H #define Idle_Loop_Unit1H //--------------------------------------------------------------------------- #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.StdCtrls.hpp> #include <Vcl.Forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TMemo *Memo1; TButton *Button1; TLabel *Label1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); unsigned int count = 0; void __fastcall IdleLoop(TObject*, bool& done); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif |
Here is a C++ VCL example file which uses an IdleLoop method
In the C++ Unit file, we can define the IdleLoop method and the processes that we want to do. We can set the done property to false
then we can do our processes. Here is the full C++ example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Idle_Loop_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Application->OnIdle = IdleLoop; } //--------------------------------------------------------------------------- void __fastcall TForm1::IdleLoop(TObject*, bool& done) { done = false; count++; Label1->Caption = UIntToStr(count); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Memo1->Lines->Add(count); count = 0; } //--------------------------------------------------------------------------- |
There is a 3D ball simulation post that has an IdleLoop event which draws the OpenGL environment when the application is Idling; please read this post for the application.
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’s 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; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.