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

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

How we can define and use C++ Builder’s idle loop on Windows? How can set a method to OnIdle method of Application? What is OnIdle ? How can I detect if my application is currently idle?

In C++ Builder, the global variable Application (TApplication), is in every VCL-based application. Application encapsulates your application as well as providing 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 C++ Builder, 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 or 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.

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

C++Introduction to C++Language FeatureLearn C++

How to Create a New Windows FMX Static Library In C++

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

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

en_USEnglish