"Matthew H" <
XXXX@XXXXX.COM>writes
Quote
Why won't this code echo back anything?
There are several mistakes with your code.
Quote
while Athread.Connection.Connected do begin
Get rid of that altogether. You should NOT be looping like that at all.
The OnExecute event is already looped by TIdTCPServer automatically, and in
fact performing that kind of loop manually interfers with TIdTCPServer's
internal connection management.
Quote
Memo.Lines.Add(sCommand);
The OnExecute event is multi-threaded. That line of code is NOT
thread-safe. You are probably deadlocking your code, that would explain the
lack of echoing. If you need to display the input commands, then you MUST
use the thread's Synchronize() method in order to access the Memo in a
thread-safe manner. Look at the TIdSync class to help you with that.
Use this code instead:
type
MemoSync = class(TIdSync)
private
FMemo: TMemo;
FStr: String;
protected
procedure DoSynchronize; override;
public
constructor Create(AThread: TIdThread; AMemo: TMemo; AStr:
String);
class procedure Add(AThread: TIdThread; AMemo: TMemo; AStr:
String);
end;
constructor MemoSync.Create(AThread: TIdThread; AMemo: TMemo; AStr:
String);
begin
inherited Create(AThread);
FMemo := AMemo;
FStr := AStr;
end;
procedure MemoSync.DoSynchronize;
begin
FMemo.Lines.Add(FStr);
end;
class procedure MemoSync.Add(AThread: TIdThread; AMemo: TMemo; AStr:
String);
begin
with MemoSync.Create(AThread, AMemo, AStr) do
try
Synchronize;
finally
Free;
end;
end;
procedure TMainForm.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
AThread.Data := TObject(1);
end;
procedure TMainForm.IdTCPServer1Execute(AThread: TIdPeerThread);
var
sCommand: string;
Stage: Integer;
begin
Stage := Integer(AThread.Data);
with AThread.Connection do try
sCommand := ReadLn;
MemoSync.Add(AThread, Memo1, sCommand);
WriteLn(IntToStr(Stage));
WriteLn(sCommand);
AThread.Data := TObject(Stage+1);
end;
end;
Gambit