Search code examples
delphiindy

Can I use Exit in TIdTCPServer.OnExecute event


Can I use Exit after adding a Queue command in the TIdTCPServer.OnExecute event?

if condition then
begin
  MyNotify          := TMyNotify.Create;
  MyNotify.FMyData  := Format('%s > %d > %s, [TimeToStr(Now), AContext.Connection.Socket.Binding.Handle, AContext.Binding.PeerIP]);
  MyNotify.Notify;

  Con.Queue.Add('DCCUSTOMER');
  exit;
end;

Or will it cause deadlocks, or other problems?


Solution

  • Yes, you can use Exit in the TIdTCPServer.OnExecute event handler.

    The OnExecute event is fired in a continuous loop for the lifetime of the TCP connection. Exiting from the OnExecute handler is perfectly normal, the event will simply be fired again. This allows you to write simpler handler code, as you only have to write code for one iteration at a time. The most common use-case is to read and process one command and then exit (implicitly or explicitly, it doesn't matter), repeating for the next command when the event is fired again.

    Closing the socket, or raising an uncaught exception, will terminate the loop.