delphiterminate

Delphi Application Run


The Application.Run procedure calls an infinite loop that handles windows messages:

  repeat
    try
      HandleMessage;
    except
      HandleException(Self);
    end;
  until Terminated;

The Terminated property can be set to true only through Application.Terminate procedure, which sends PostQuitMesage. I would like to change the message handling loop so that I can directly stop it using the global variable (without using messages queue):

var MyTerminated:Boolean
....
  repeat
    try
      HandleMessage;
    except
      HandleException(Self);
    end;
  until Terminated or MyTerminated;

The question is, is it possible to make the program use your own version of Application.Run?


Solution

  • "Terminated" property is read-only. However it is a direct getter of the FTerminated field, therefore Application.Terminated directly reads from the boolean field. While the language disallows setting Application.Terminated, you can set the boolean value at that address using a pointer:

    PBoolean(@Application.Terminated)^ := True;
    

    You may also consider using Halt, which will pass over the message loop completely, for a more abrupt but less hacky solution.