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

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How To Define And Use An Idle Loop In A Windows Application
By Yilmaz Yoru September 21, 202

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

Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся 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,
C++:
__property TIdleEvent OnIdle = {read=FOnIdle, write=FOnIdle};
we can declare new OnIdle Idle Loop method. Here is a syntax for the IdleLoop example below,
C++:
void __fastcall IdleLoop(TObject*, bool& done);
We can define IdleLoop this method as below,
C++:
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,
C++:
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,
C++:
//---------------------------------------------------------------------------
 
#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,
C++:
//---------------------------------------------------------------------------
 
#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;
}
//---------------------------------------------------------------------------