code:
class procedure TThreadSynchronisationHandler.Update(Sender: TObject);
begin
CheckSynchronize(); //This has to be done in the main thread regularly to enable Synchronisation between threads.
end;
class procedure TThreadSynchronisationHandler.Initialise;
begin
if not IsInitialised then
begin
ApplicationProperties.OnUpdate.Add({$ifdef FPC}@{$endif}Update);
IsInitialised := true;
end;
end;
procedure TCastleTCPClientThread.Execute;
var
LMessage: String;
begin
FClient.Connect;
FClient.IOHandler.ReadTimeout := 100;
if Assigned(FOnConnected) then
Queue(FOnConnected);
while not Terminated do
begin
try
LMessage := FClient.IOHandler.ReadLn; //Result is empty when timeout reached.
if LMessage <> '' then
begin
FMessageList.Add(LMessage);
Queue(FOnMessageReceived);
end;
except
//Exception occurs when connection was disconnected (gracefully).
Terminate;
if Assigned(FOnDisconnected) then
Synchronize(FOnDisconnected); //Must be synchronize, not queue, because queued messages get lost.
end;
end;
FClient.Disconnect;
if FreeClientOnTerminate then
FClient.Free;
end;