How to stop the OnExecute TCPServer event from endless execution after AContext.Connection.Disconnect?

I have this event TCPServerExecutethat I want to terminate after execution after I manually disconnect the client connection:

procedure TMainForm.TCPServerExecute(AContext: TIdContext);
var
  TCPClient : TIdTCPClient;
begin
try
  TCPClient := nil;
  try
    TCPClient := TIdTCPClient.Create(nil);
    if aConditionIsMet then begin 
      AContext.Connection.IOHandler.WriteLn('Disconnected from server.');
      AContext.Connection.Disconnect;
      Exit;
    end;
  finally
    FreeAndNil(TCPClient);
  end;
except on e : Exception do
  begin
    MainForm.Log('error in Execute=' + e.Message);
  end;
end;
end;

and on the client side everything is fine, but on the server side, I loop through TCPServerExecute endlessly. What am I doing wrong and how can I stop execution TCPServerExecuteafter input AContext.Connection.Disconnect?

+4
source share
1 answer

The loop continues because Indy exceptions are not handled correctly.

Either remove the exception handler, or rerun the exception after logging:

except 
  on e : Exception do
  begin
    MainForm.Log('error in Execute=' + e.Message);
    raise;
  end;
end;

p.s. MainForm . (TThread.Queue ).

+2

Source: https://habr.com/ru/post/1541134/


All Articles