Board index » delphi » Theoretical question about program-flow.

Theoretical question about program-flow.

Hi!
Windows and Delphi are event-driven, but what exactly does that mean?
Let us say I have a program that runs in a loop like this:'

procedure TForm1.StartButtonClick(Sender : TObject);
Begin
    StopFlag := False;
    while not StopFlag do
    Begin
        Application.ProcessMessages;
        DoLine1;
        DoLine2;
        DoLine3;
    End;
    DoCleanUp;
End;

I can then have a Stop-button like this:

procedure TForm1.StartButtonClick(Sender : TObject);
Begin
    StopFlag := True;
End;

That works okay - when I press the stop button, the main procedure finishes
gracefully.
BUT - what if I the user tryes to close my Application, while it is running
in the loop?

I could have a FormCloseQuery like this:

TForm1.FormCloseQuery(Sender : TObject; var CanClose: Boolean);
Begin
    StopFlag := True;
    CanClose := True;
End;

Let us say the program is about to execute DoLine2, when it receives the
CloseQuery message. Then it executes the FormCloseQuery, but: IT DOES NEVER
FINISH THE STARTBUTTONCLICK PROCEDURE.
Now, is that correct, and if it is, how can I make it finish the procedure
before closing down?
Thanks for any comments on this.
--
Yours

Poul Bak
(Don't get Bit-hysterical, remember 2 bits are just a little bit more than
1).

 

Re:Theoretical question about program-flow.


Quote
>Hi!
>Windows and Delphi are event-driven, but what exactly does that mean?
>Let us say I have a program that runs in a loop like this:'

>procedure TForm1.StartButtonClick(Sender : TObject);
>Begin
>    StopFlag := False;
>    while not StopFlag do
>    Begin
>        Application.ProcessMessages;
>        DoLine1;
>        DoLine2;
>        DoLine3;
>    End;
>    DoCleanUp;
>End;

>I can then have a Stop-button like this:

>procedure TForm1.StartButtonClick(Sender : TObject);
>Begin
>    StopFlag := True;
>End;

>That works okay - when I press the stop button, the main procedure finishes
>gracefully.
>BUT - what if I the user tryes to close my Application, while it is running
>in the loop?

>I could have a FormCloseQuery like this:

>TForm1.FormCloseQuery(Sender : TObject; var CanClose: Boolean);
>Begin
>    StopFlag := True;
>    CanClose := True;
>End;

>Let us say the program is about to execute DoLine2, when it receives the
>CloseQuery message. Then it executes the FormCloseQuery, but: IT DOES NEVER
>FINISH THE STARTBUTTONCLICK PROCEDURE.
>Now, is that correct, and if it is, how can I make it finish the procedure
>before closing down?
>Thanks for any comments on this.

Just an idea:

TForm1.FormCloseQuery(Sender : TObject; var CanClose: Boolean);
Begin
    CanClose := StopFlag;
    StopFlag := True;
    ExitFlag := true;
End;

procedure TForm1.StartButtonClick(Sender : TObject);
Begin
    ExitFlag := false;
    StopFlag := False;
    while not StopFlag do
    Begin
        Application.ProcessMessages;
        DoLine1;
        DoLine2;
        DoLine3;
    End;
    DoCleanUp;
    if ExitFlag then Close;
End;

  /Anders

------------------------------------------------------------------------
Anders S?dergrann      
t97...@student.tdb.uu.se.killmail.com
a...@sfr.se.killmail.com
UIN: 374439
------------------------------------------------------------------------
                 ** You're probably an engineer **
If you're aware that computers are actually only good for playing games,
                 but are afraid to say it out loud
---------------------------------------------------------------^nizze^--

Re:Theoretical question about program-flow.


Delphi will finish the procedure - it will remain in the loop until
StopFlag=True AND it arrives at the control statement that tests for
this. It only tests the Stop Flag at your WHILE statement, so once it
processes the messages, even though stop flag is true, it will go on
to DoLine1, DoLine2 and DoLine3, then will return to the WHILE
statement to check, will find that StopFlag = true, and will break
from the loop.

On Sat, 20 Nov 1999 14:51:42 +0100, "Poul S. Bak"

Quote
<poul.s....@get2net.dk> wrote:
>Hi!
>Windows and Delphi are event-driven, but what exactly does that mean?
>Let us say I have a program that runs in a loop like this:'

>procedure TForm1.StartButtonClick(Sender : TObject);
>Begin
>    StopFlag := False;
>    while not StopFlag do
>    Begin
>        Application.ProcessMessages;
>        DoLine1;
>        DoLine2;
>        DoLine3;
>    End;
>    DoCleanUp;
>End;

>I can then have a Stop-button like this:

>procedure TForm1.StartButtonClick(Sender : TObject);
>Begin
>    StopFlag := True;
>End;

>That works okay - when I press the stop button, the main procedure finishes
>gracefully.
>BUT - what if I the user tryes to close my Application, while it is running
>in the loop?

>I could have a FormCloseQuery like this:

>TForm1.FormCloseQuery(Sender : TObject; var CanClose: Boolean);
>Begin
>    StopFlag := True;
>    CanClose := True;
>End;

>Let us say the program is about to execute DoLine2, when it receives the
>CloseQuery message. Then it executes the FormCloseQuery, but: IT DOES NEVER
>FINISH THE STARTBUTTONCLICK PROCEDURE.
>Now, is that correct, and if it is, how can I make it finish the procedure
>before closing down?
>Thanks for any comments on this.
>--
>Yours

>Poul Bak
>(Don't get Bit-hysterical, remember 2 bits are just a little bit more than
>1).

Re:Theoretical question about program-flow.


Aren't al your procedure stopped when you close your app?
+the KING

Poul S. Bak <poul.s....@get2net.dk> schreef in berichtnieuws
8169em$nd...@news.inet.tele.dk...

Quote
> Hi!
> Windows and Delphi are event-driven, but what exactly does that mean?
> Let us say I have a program that runs in a loop like this:'

> procedure TForm1.StartButtonClick(Sender : TObject);
> Begin
>     StopFlag := False;
>     while not StopFlag do
>     Begin
>         Application.ProcessMessages;
>         DoLine1;
>         DoLine2;
>         DoLine3;
>     End;
>     DoCleanUp;
> End;

> I can then have a Stop-button like this:

> procedure TForm1.StartButtonClick(Sender : TObject);
> Begin
>     StopFlag := True;
> End;

> That works okay - when I press the stop button, the main procedure
finishes
> gracefully.
> BUT - what if I the user tryes to close my Application, while it is
running
> in the loop?

> I could have a FormCloseQuery like this:

> TForm1.FormCloseQuery(Sender : TObject; var CanClose: Boolean);
> Begin
>     StopFlag := True;
>     CanClose := True;
> End;

> Let us say the program is about to execute DoLine2, when it receives the
> CloseQuery message. Then it executes the FormCloseQuery, but: IT DOES
NEVER
> FINISH THE STARTBUTTONCLICK PROCEDURE.
> Now, is that correct, and if it is, how can I make it finish the procedure
> before closing down?
> Thanks for any comments on this.
> --
> Yours

> Poul Bak
> (Don't get Bit-hysterical, remember 2 bits are just a little bit more than
> 1).

Re:Theoretical question about program-flow.


Hi!

If you wrap your loop into a try..finally construct, then the bit in the
finally-section always gets executed. So put the DoCleanUp call into the
finally-section.
I use D2 and D3 and that has so far always worked for me.

Doswidanja,
hannes

Re:Theoretical question about program-flow.


Yes, all my procedures are stopped. But the main procedure will never finish
!

King Marcel <k...@dds.nl> skrev i en
nyhedsmeddelelse:819dgo$80u...@reader3.wxs.nl...

Quote
> Aren't al your procedure stopped when you close your app?
> +the KING

> Poul S. Bak <poul.s....@get2net.dk> schreef in berichtnieuws
> 8169em$nd...@news.inet.tele.dk...
> > Hi!
> > Windows and Delphi are event-driven, but what exactly does that mean?
> > Let us say I have a program that runs in a loop like this:'

> > procedure TForm1.StartButtonClick(Sender : TObject);
> > Begin
> >     StopFlag := False;
> >     while not StopFlag do
> >     Begin
> >         Application.ProcessMessages;
> >         DoLine1;
> >         DoLine2;
> >         DoLine3;
> >     End;
> >     DoCleanUp;
> > End;

> > I can then have a Stop-button like this:

> > procedure TForm1.StartButtonClick(Sender : TObject);
> > Begin
> >     StopFlag := True;
> > End;

> > That works okay - when I press the stop button, the main procedure
> finishes
> > gracefully.
> > BUT - what if I the user tryes to close my Application, while it is
> running
> > in the loop?

> > I could have a FormCloseQuery like this:

> > TForm1.FormCloseQuery(Sender : TObject; var CanClose: Boolean);
> > Begin
> >     StopFlag := True;
> >     CanClose := True;
> > End;

> > Let us say the program is about to execute DoLine2, when it receives the
> > CloseQuery message. Then it executes the FormCloseQuery, but: IT DOES
> NEVER
> > FINISH THE STARTBUTTONCLICK PROCEDURE.
> > Now, is that correct, and if it is, how can I make it finish the
procedure
> > before closing down?
> > Thanks for any comments on this.
> > --
> > Yours

> > Poul Bak
> > (Don't get Bit-hysterical, remember 2 bits are just a little bit more
than
> > 1).

Re:Theoretical question about program-flow.


Hi!
You might be right, but I think Anders S?dergrann gave a more elegant
solution.

Yours

Poul Bak

Hannes Breuer <hannes.bre...@talknet.de> skrev i en
nyhedsmeddelelse:3839A706.A09B6...@talknet.de...

Quote
> Hi!

> If you wrap your loop into a try..finally construct, then the bit in the
> finally-section always gets executed. So put the DoCleanUp call into the
> finally-section.
> I use D2 and D3 and that has so far always worked for me.

> Doswidanja,
> hannes

Re:Theoretical question about program-flow.


In the case that the message is 'WM_CloseQuesy' then it will not finish the
main loop - or it will not close the application. However, Anders S?dergrann
gave the solution.

Tom <tomval...@volny.cz> skrev i en
nyhedsmeddelelse:3836c7da.30910...@news.volny.cz...

Quote
> Delphi will finish the procedure - it will remain in the loop until
> StopFlag=True AND it arrives at the control statement that tests for
> this. It only tests the Stop Flag at your WHILE statement, so once it
> processes the messages, even though stop flag is true, it will go on
> to DoLine1, DoLine2 and DoLine3, then will return to the WHILE
> statement to check, will find that StopFlag = true, and will break
> from the loop.

> On Sat, 20 Nov 1999 14:51:42 +0100, "Poul S. Bak"
> <poul.s....@get2net.dk> wrote:

> >Hi!
> >Windows and Delphi are event-driven, but what exactly does that mean?
> >Let us say I have a program that runs in a loop like this:'

> >procedure TForm1.StartButtonClick(Sender : TObject);
> >Begin
> >    StopFlag := False;
> >    while not StopFlag do
> >    Begin
> >        Application.ProcessMessages;
> >        DoLine1;
> >        DoLine2;
> >        DoLine3;
> >    End;
> >    DoCleanUp;
> >End;

> >I can then have a Stop-button like this:

> >procedure TForm1.StartButtonClick(Sender : TObject);
> >Begin
> >    StopFlag := True;
> >End;

> >That works okay - when I press the stop button, the main procedure
finishes
> >gracefully.
> >BUT - what if I the user tryes to close my Application, while it is
running
> >in the loop?

> >I could have a FormCloseQuery like this:

> >TForm1.FormCloseQuery(Sender : TObject; var CanClose: Boolean);
> >Begin
> >    StopFlag := True;
> >    CanClose := True;
> >End;

> >Let us say the program is about to execute DoLine2, when it receives the
> >CloseQuery message. Then it executes the FormCloseQuery, but: IT DOES
NEVER
> >FINISH THE STARTBUTTONCLICK PROCEDURE.
> >Now, is that correct, and if it is, how can I make it finish the
procedure
> >before closing down?
> >Thanks for any comments on this.
> >--
> >Yours

> >Poul Bak
> >(Don't get Bit-hysterical, remember 2 bits are just a little bit more
than
> >1).

Re:Theoretical question about program-flow.


Hi !
I like your solution, it's simple and it works.
When the user tryes to close my application (or close Windows) my main loop
will finish correctly (as if the STOP-button was pressed) AND the
application wil close.
All in all, beatiful programming.

Yours
Poul Bak

Anders S?dergrann <a...@sfr.se.killmail.com> skrev i en
nyhedsmeddelelse:q7g2OL3ESzzugT0pY+g=zPBDb...@4ax.com...

Quote
> >Hi!
> >Windows and Delphi are event-driven, but what exactly does that mean?
> >Let us say I have a program that runs in a loop like this:'

> >procedure TForm1.StartButtonClick(Sender : TObject);
> >Begin
> >    StopFlag := False;
> >    while not StopFlag do
> >    Begin
> >        Application.ProcessMessages;
> >        DoLine1;
> >        DoLine2;
> >        DoLine3;
> >    End;
> >    DoCleanUp;
> >End;

> >I can then have a Stop-button like this:

> >procedure TForm1.StartButtonClick(Sender : TObject);
> >Begin
> >    StopFlag := True;
> >End;

> >That works okay - when I press the stop button, the main procedure
finishes
> >gracefully.
> >BUT - what if I the user tryes to close my Application, while it is
running
> >in the loop?

> >I could have a FormCloseQuery like this:

> >TForm1.FormCloseQuery(Sender : TObject; var CanClose: Boolean);
> >Begin
> >    StopFlag := True;
> >    CanClose := True;
> >End;

> >Let us say the program is about to execute DoLine2, when it receives the
> >CloseQuery message. Then it executes the FormCloseQuery, but: IT DOES
NEVER
> >FINISH THE STARTBUTTONCLICK PROCEDURE.
> >Now, is that correct, and if it is, how can I make it finish the
procedure
> >before closing down?
> >Thanks for any comments on this.

> Just an idea:

> TForm1.FormCloseQuery(Sender : TObject; var CanClose: Boolean);
> Begin
>     CanClose := StopFlag;
>     StopFlag := True;
>     ExitFlag := true;
> End;

> procedure TForm1.StartButtonClick(Sender : TObject);
> Begin
>     ExitFlag := false;
>     StopFlag := False;
>     while not StopFlag do
>     Begin
>         Application.ProcessMessages;
>         DoLine1;
>         DoLine2;
>         DoLine3;
>     End;
>     DoCleanUp;
>     if ExitFlag then Close;
> End;

>   /Anders

> ------------------------------------------------------------------------
> Anders S?dergrann
> t97...@student.tdb.uu.se.killmail.com
> a...@sfr.se.killmail.com
> UIN: 374439
> ------------------------------------------------------------------------
>                  ** You're probably an engineer **
> If you're aware that computers are actually only good for playing games,
>                  but are afraid to say it out loud
> ---------------------------------------------------------------^nizze^--

Other Threads