Board index » delphi » Delphi Guru - Correct Termination of a program (D1)

Delphi Guru - Correct Termination of a program (D1)

Could someone please tell me the technically correct way of terminating a
program's execution (using Delphi 1.0). I usually use the MainForm.Close
method but if the application is currently busy (say in a loop) then the
form doesn't close.

I have tried many different methods all of which seem to have major
shortcomings. (Application.Terminate; Halt; Exit; Abort etc etc). Ideally I
need a method which still frees all memory allocated automatically but also
breaks out of current execution.

Can someone with a good knowledge of Delphi please help.

Reply to newsgroup but cc: to madk...@tpgi.com.au is really appreciated.

Thanks in advance.

Cheers. Michael.

 

Re:Delphi Guru - Correct Termination of a program (D1)


How about this... Have a global variable that is set to True when the
application is asked to close. Check this variable in your looping
constructs and raise an exception if the global is set to True. In the
handler for the exception, send a Close message to the main form. This is
just off the top of my head, but it would probably work. Can't think of any
*obvious* disadvantages, can you? BTW, are you calling
Application.ProcessMessages in your loops?
--
Trey Campbell
campb...@softdisk.com

Michael Adkins <madk...@tpgi.com.au> wrote in article
<01bc0293$9a7777a0$4e2219cb@mick>...

Quote
> Could someone please tell me the technically correct way of terminating a
> program's execution (using Delphi 1.0). I usually use the MainForm.Close
> method but if the application is currently busy (say in a loop) then the
> form doesn't close.

> I have tried many different methods all of which seem to have major
> shortcomings. (Application.Terminate; Halt; Exit; Abort etc etc). Ideally
I
> need a method which still frees all memory allocated automatically but
also
> breaks out of current execution.

> Can someone with a good knowledge of Delphi please help.

> Reply to newsgroup but cc: to madk...@tpgi.com.au is really appreciated.

> Thanks in advance.

> Cheers. Michael.

Re:Delphi Guru - Correct Termination of a program (D1)


Quote
Michael Adkins wrote:

> Could someone please tell me the technically correct way of terminating a
> program's execution (using Delphi 1.0). I usually use the MainForm.Close
> method but if the application is currently busy (say in a loop) then the
> form doesn't close.

> I have tried many different methods all of which seem to have major
> shortcomings. (Application.Terminate; Halt; Exit; Abort etc etc). Ideally I
> need a method which still frees all memory allocated automatically but also
> breaks out of current execution.

> Can someone with a good knowledge of Delphi please help.

> Reply to newsgroup but cc: to madk...@tpgi.com.au is really appreciated.

> Thanks in advance.

> Cheers. Michael.

I usually make sure that the application is NEVER inside any
un-conditional loop unless it is absolutely necessary.  But if you do
need it, try the following:

Place a global EXIT STATUS variable and initialize it to a value, let's
FALSE.  No inside your loop (assuming Pascal) add an IF/EXIT statement
like:

    for I := 1 to 90000 do
       begin

         // Do whatever is necessary.....

         Application.ProcessMesssages;

         if EXIT_STATUS = TRUE then  
            break;

       end;    

    // Of course, you can put the condition checking along with the
WHILE loop expression
    // if you using WHILE or UNTIL loop.

In your Form.Close event, you should set the EXIT_STATUS a value of
TRUE.  This way, the FOR loop would naturally breaks and with necessary
agjustment, you should be able to terminate the application without even
making a Terminate, Halt or any calls.

Good Luck,

Re:Delphi Guru - Correct Termination of a program (D1)


Michael,

Seeing the other responses I might add another one:

while (something) do
  begin
    application.processmessages;
    if application.terminated then exit
    else do_your_stuff;
  end;

You don't need to set any flags, VCL does it for you!
It is more difficult if the time-consuming task is out of your control, ie.
a database query or third-party library call. In D2 you could place such
operation in a separate thread, but D1 is defenceless.

HTH
Peter Szymiczek

Michael Adkins <madk...@tpgi.com.au> wrote in article
<01bc0293$9a7777a0$4e2219cb@mick>...

Quote
> Could someone please tell me the technically correct way of terminating a
> program's execution (using Delphi 1.0). I usually use the MainForm.Close
> method but if the application is currently busy (say in a loop) then the
> form doesn't close.

> I have tried many different methods all of which seem to have major
> shortcomings. (Application.Terminate; Halt; Exit; Abort etc etc). Ideally
I
> need a method which still frees all memory allocated automatically but
also
> breaks out of current execution.

> Can someone with a good knowledge of Delphi please help.

> Reply to newsgroup but cc: to madk...@tpgi.com.au is really appreciated.

> Thanks in advance.

> Cheers. Michael.

Other Threads