I have a simple Delphi program that I am working on, in which I am trying to use threads to separate the program’s functionality from its GUI, and also support the GUI during longer tasks, etc. I have a "controller" TThread and a "view" TForm. The view knows the handle of the controller that it uses to send controller messages through PostThreadMessage. In the past, I had no problem using such a model for forms that are not the main form, but for some reason, when I try to use this model for the main form, the message loop in the stream simply ends.
Here is my code for the thread message loop:
procedure TController.Execute;
var
Msg : TMsg;
begin
while not Terminated do begin
if (Integer(GetMessage(Msg, hwnd(0), 0, 0)) = -1) then begin
Synchronize(Terminate);
end;
TranslateMessage(Msg);
DispatchMessage(Msg);
case Msg.message of
// ...call different methods based on message
end;
end;
end;
To configure the controller, I do the following:
Controller := TController.Create(true);
Controller.FreeOnTerminate := True;
Controller.Resume;
Application.Run, ( Controller.Resume)
while not Application.Terminated do begin
Application.ProcessMessages;
end;
- .