0

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
PSDEVS
  • 57
  • 4
  • 1
    Please provide a [mcve] that demonstrates the actual issue you're asking about. In the out of context snippet of code you've posted here, the presence of `exit` makes absolutely no difference. It might if we could see the rest of the code around this small bit, though. Without the rest of that code, it's impossible to say whether `exit` in that spot matters or not. – Ken White Aug 22 '19 at 23:25
  • This question is akin to asking if it is possible for the event handler to return ever. – David Heffernan Aug 23 '19 at 07:45

1 Answers1

2

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770