Advanced question about program-flow and 'Windows-shut down'.

Hi!
Say I have a program that runs in a loop like this: (Thanks to Anders
S?dergrann for help on that)

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;

and a FormCloseQuery like this:

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

When a user wants to close my application, FormCloseQuery first returns
False, then my main loop will finish correctly and call Close again to close
my application (the second call to FormCloseQuery will return True).
So far, so good!
But what if the user shuts down or restarts windows. I can do the same with
QueryEndSession, but in the main loop I then have to either shut down or
restart Windows. To do that I have to know if the original QueryEndSession
message was part of a 'Shut down Windows' or a 'Restart Windows' message.
The big question: How can I know if a QueryEndSession comes from a
'Shut-down' or 'Restart' message.
--
Yours

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