Mega Code Archive

 
Categories / Delphi / VCL
 

Using the applications onidle event

Question: How do I have my application perform work when the computer is idle? Answer: 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 then implementation section, define the procedure: procedure TForm1.IdleEventHandler(Sender: TObject; var Done: Boolean); begin {Do a small bit of work here} Done := false; end; 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.