[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionHere'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 & TechniquesI 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
Suggestions for Further Explorations
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |