Help Newbie Understand Event Loop

In article <4fv2sa$> (Scud Hudson) writes:
>My son and I are just learning Delphi and we have written a game that
>has some logic to try and solve the puzzle so to speak.  When the
>button is pushed that invokes this rather lengthy (timewise)
>procedure, the system does not respond to mouse clicks on other
>So, how can I add some code similar to the old PeekMessage stuff that
>one might use in programming a Windows application in C?  That is, is
>there a simple way to put some code in my for loop that says, "Hey,
>stupid, check for events requested outside of this application"?  Or
>any other way to get the process to run in a true background mode.
>If you reply here, please respond via e-mail too.  I don't come here
>as often as I should.

Here's what I would do:  

{1}  Define a button on the form, which will be enabled when you are searching
for a solution to the puzzle.  It says "Stop!" and when pushed, it sets a
boolean flag... "stopRequested := True."

{2}  When your program starts searching, it sets "stopRequested := false" and
then "stopButton.Enabled := True."

{3}  Periodically while searching your program includes the following

        if stopRequested then abort;   { or some other appropriate response }

{4}  Elsewhere in the program include the following routine:

        procedure DoEvents;
           if application.Terminated then abort;
           if application.Terminated then abort;

What this routine does is the following:
        (a)  It checks to see if the user has previously tried to close the program.
        (b)  If not, it processes all Windows messages now in the queue.
        (c)  Finally, it checks to see if one of those events tried to close.

Any combination of ways to close the program, including double-clicking on the
"close" box of the main window, will cause the program to terminate cleanly.  
But having said that, here's what happens when the user gets tired and wants
to stop:

        (a)  User clicks on the stop-button.
        (b)  When you call "doEvents," the button-click message gets processed.
        (c)  The onClick routine sets "stopRequested."
        (d)  Your program senses the request and stops searching.