Search code examples
delphiindy

How to use visual controls in Indy? (Delphi)


My server must print some reports about its work. How can I use visual objects such as labels, edit boxes in the OneEecute event?


Solution

  • The same rule, for not modifying VCL objects in any thread except main thread, is valid here too. You should not change any of VCL controls in OnExecute event-handler, because that code will be run in the context of a worker thread spawn by Indy for every connection.

    If you need to change graphical user interface, do it using Synchronize or Queue methods, or use a custom notification mechanism for notifying the main thread to do the GUI modification for you.

    If you want to call Synchronize or Queue methods, you have to type-cast TIdYarn to TIdYarnOfThread which derives from TIdYarn and implements it with threads:

    // Calling MyMethod using Synchornize inside TIdTcpServer.OnExecute event-handler   
    TIdYarnOfThread(AContext.Yarn).Thread.Synchronize(MyMethod);
    
    
    // Calling MyMethod using Queue inside TIdTcpServer.OnExecute event-handler  
    TIdYarnOfThread(AContext.Yarn).Thread.Queue(MyMethod);