Calling TIdTCPClient.IOHandler.Write from main thread lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- PawelO · 2025-07-30 15:17 Hello, Recently, I found in this thread: https://en.delphipraxis.net/topic/1010-using-indy-for-cross-platform- tcpip/ that calling TIdTCPClient.IOHandler.Write... (which is something that I've been doing since forever - thanks to almost every tutorial - and that seems to work fine) from main thread can be "unsafe" and it's not recommended. However, I cannot call it from the thread I have already created and used for listening (TIdTCPClient.IOHandler.Read...), since this thread is usually blocked waiting for incoming messages. Do I need a third thread then? Or can I use same thread, sequentially reading with some small timeout (but how small can it be), and writing from queue? I basically want to create a TCPClient wrapper that has Connect, Disconnect, WriteLn methods, some OnReadLn event and Connected property all working with main (UI) thread, in an asynchronous, non- blocking way. Are somewhere examples how to do it in the most simple and "right" way? Best regards ---------------------------------------------------------------------- Remy Lebeau · 2025-07-30 17:13 Quote from: PawelO on July 30, 2025, 05:17:49 pmDo I need a third thread then? Or can I use same thread, sequentially reading with some small timeout (but how small can it be), and writing from queue? Basically, yes.  Either approach will work. Use whichever one makes more sense for your app's design. Quote from: PawelO on July 30, 2025, 05:17:49 pmI basically want to create a TCPClient wrapper that has Connect, Disconnect, WriteLn methods, some OnReadLn event and Connected property all working with main (UI) thread, in an asynchronous, non-blocking way. Then perhaps Indy is not the best choice for your app. Since Indy is primarily a synchronous library, threads and timeouts are basically your only options for asynchronous behaviors. Otherwise, look into other options if you need truly asynchronous sockets. Quote from: PawelO on July 30, 2025, 05:17:49 pmAre somewhere examples how to do it in the most simple and "right" way? Nothing official, no.  Just posts floating around the various forum sites over the years.