Search code examples
delphiindyindy10

TIdCommandHandler: OnCommand wait terminate


I have a server using TIdCmdTCPServer and several clients. Those clients could send messages at the same time. When I simulate this (put 10 clients to send a command at the same time for example) the OnCommand event of TIdCommandHandler is called, but before it terminates, it's interrupted and it's called again by other client, and so on.

So, is there a way to configures OnCommand from TIdCommandHandler to finish the event before be called again?


Solution

  • Like most other servers in Indy, TIdCmdTCPServer is a multi-threaded component. Each connected client is managed by its own worker thread. The OnCommand event handler is called in the context of each thread that receives a client's command. It is thus possible for multiple OnCommand handlers to be running in parallel at the same time, so your event handler code must be written in a thread-safe manner. You should not try to serialize the events themselves, only serialize access to any shared resources that the events access across thread boundaries.