OnIdle Exit Demo

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only




Problem Description

 Here's a simple program which demonstrates how to use the Application.OnIdle event exit to perform background processing while your user carries on with whatever, 

Background & Techniques

I have used this a few times in the past to calculate  answers to puzzles while the user worked on them manually.  The results were then either used to to display results quickly or to provide user hints when requested.

The OnIdle procedure must set a Boolean parameter to tell the application message processing loop whether to call it again the next time through if he has no messages waiting.  The danger of OnIdle processing is that you can monopolize the available CPU time and lock the user out until the  calculations are complete.   However, by keeping track of where we are in the background calculations, we can safely steal a few milliseconds worth of processing at a time without impacting the user at all.    The specifics of how we divide the background task into smaller chunks depends on the problem.  If we are checking permutations in a search, we can one permutation per entry,  If, as in this demo, we need to examine the digits of 100,000,000  5-digit integers, we can process a few thousand on each entry and keep track of how many have been already checked to set our next starting point. 

I have provided two buttons  plus a TMemo for user entry on the form.  The purpose of the memo is to demonstrate that users can enter text as the average digit value for 100 million 5-digit integers is being calculated.  The first button performs the calculation in the foreground and effectively locks out any user actions until it is complete.  The second button assigns  the MyOnIdle procedure address to the Application.OnIdle event exit. 

Procedure SumDigits(N) calculates the total digit values, TotalSoFar, from integer NbrTested+1 to NbtTested+N. The  foreground calculation button calls SumDigits(10000000) while the background button calls SumDigits(10000).  Since calculating all 100 million takes leas than 10 seconds, the rate is about 10 million per second or 10,000 per millisecond.  By summing the digits for 10,000 integers per entry we are delaying the user by about a millisecond at a time, providing plenty of chances for you to edit some text in the memo during the 8 seconds or so it takes for the calculations. 


Running/Exploring the Program 

bulletDownload source

Suggestions for Further Explorations



Original:  June 5, 2011, 2011

Modified:  May 12, 2018

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.