I’m using a form with an IdTCPServer on it managing strings from the client with a AThread.connection.readln/writeln system. The string handling works and that isn’t the problem.
The thing is, the form with the server on it hangs and will not load, but it still managed all the clients connected to it so it IS running but it just doesn’t work as a form.
I’ll make a guess that its sitting on a readline or something… but I have NO idea how i can fix this at this moment in time.
Please help.
procedure TMonitorFrm.ServerExecute(AThread: TIdPeerThread);
procedure post(PostMessage:string);
begin
try
AThread.Connection.WriteLn(PostMessage);
except
showmessage('Cannot post');
end;
end;
var
ActClient : PClient;
sTemp,
CommBlock,
NewCommBlock,
ReceiverName,
sContent,
sSQL,
sCommand : String;
iCount2,
iCount : Integer;
sldb : TSQLiteDatabase;
sltb : TSQLiteTable;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
CommBlock := AThread.Connection.ReadLn();
ActClient := PClient(AThread.Data);
ActClient.LastAction := Now;
sCommand := copy(CommBlock,0,pos(',',CommBlock)-1); {seperate command}
sContent := copy(CommBlock,pos(',',CommBlock)+1,length(CommBlock)-(pos(',',CommBlock)+1)); {seperate data block}
iCount:= 0 ;
if sCommand = 'Announce' then //SPECIAL
begin
{ Do stuff for this command...}
end
else if sCommand = 'CheckSect' then
{Etcetera...}
procedure TMonitorFrm.FormCreate(Sender: TObject);
var
sCompetitionID : string;
sldb : TSQLiteDatabase;
sltb : TSQLiteTable;
begin
Clients := TThreadList.Create;
Server.Active := True;
AreaPnlList := TComponentList.Create;
SectionPnlList := TComponentList.Create;
Repeat until InputQuery('Competition Select', 'Please type the ID of the competition', sCompetitionID);
iCompetitionID:=StrToInt(sCompetitionID);
OpenDatabase(slDb);
sltb:=slDb.GetTable('SELECT * FROM SectionTable WHERE CompetitionID='+sCompetitionID);
Frame31.CreateSections(sltb,Frame31);
sltb.Free;
CloseDatabase(slDb);
{
This section needs to check the SQLite databases for sections and list them in the display window and makes a drag n drop profile...
}
end;
This is how to stop the Readln causing it to freeze. You need to check if there is something there to read first. There are a couple ways to do this, you can make the readln timeout
Or you can check if the connection is readable before tryign to read.
Either way, the key line is Application.ProcessMessages as this allows the main thread to check its messages and keep running.