TIdTCPServer
is a multithreaded component. Accessing the UI directly from within its OnConnect
event (or OnDisconnect
, OnExecute
, or OnException
) is not thread-safe! You need to use Indy's TIdSync
or TIdNotify
class to access the UI safely.
To answer the original question, the simpliest, but not necessarily the safest, way is to store the TIdContext.Connection
object pointer in the TListItem.Data
property. The main thread code will then have direct access to the connection when it needs it.
I do not advise that, though. A safer solution is to uniquely identify each client yourself, such as with a username that the client sends, and store that ID in the TIdContext.Data
and TListItem.Data
properties. Then, when your main thread code wants to send a message to a client, it can loop through the TIdTCPServer.Contexts
list looking for the desired ID, and if found then it will have access to the corresponding TIdContext.Connection
object.