C++Game DevelopmentIntroduction to C++Language FeatureLearn C++

How To Define And Use An Idle Loop In A Windows Application

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++.

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,

we can declare new OnIdle Idle Loop method. Here is a syntax for the IdleLoop example below,

We can define IdleLoop this method as below,

Finally we can set this IdleLoop method to OnIdle property of our Application as below,

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,

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,

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.

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.

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++?