Главная страница | назад

Article #16635: doing work while application is Idle

 Question and Answer Database

FAQ1635D.txt   doing work while application is Idle
Category   :VCL
Platform    :All
Product    :All 32 bit  

How do I have my application perform work when it is idle?

You can create an OnIdle event to be called whenever your
application is waiting for messages to be processed. Simply 
declare the event handler procedure in the private section of 
the forms declaration:

{ Private declarations }
procedure IdleEventHandler(Sender: TObject; var Done: Boolean);

In the implementation section, define the procedure:

procedure TForm1.IdleEventHandler(Sender: TObject;
  var Done: Boolean);
 {Do a small bit of work here}
  Done := false;

Then assign the Application's OnIdle event to point to your
new procedure(You may do this whereever you like, but a good 
place would be the Forms OnCreate method):

Application.OnIdle := IdleEventHandler;

OnIdle is called only once, as the application transitions into 
an idle state. It is not called continuiously unless Done is 
set to False.  If Done is False, WaitMessage is not called. 
Applications that set Done to False consume an inordinate 
amount of CPU time that affects overall system performance.

You can set the boolean variable "done" to true when you want 
to stop the event handler from executing.

7/16/98 4:31:28 PM

Last Modified: 01-SEP-99