Search code examples
multithreadingdelphiindylazarus

Change Indy10 TCP ServerExecute thread priority for faster speed of execution?


Normally when using Delphi/Lazarus threads it is possible to change the priority of the thread so that it can execute faster.

That said, is it possible to do the same for the Indy10 TCP Server component? If so, how? Is it even advisable to do so?

thanks


Solution

  • TIdTCPServer uses normal Delphi/Lazarus threads.

    If you are running on Windows, you can call the Win32 API GetCurrentThread() and SetThreadPriority() functions. Or, you can set the priority of a TThread object using Indy's SetThreadPriority() (Indy 9 and earlier) or IndySetThreadPriority() (Indy 10) function.

    If you are using a modern Delphi version, you can use the TThread.CurrentThread property to get a TExternalThread object representing the calling thread. TExternalThread is a TThread descendant, so it can be passed to IndySetThreadPriority(), or you can just set the TThread.Priority property directly.

    If you want to access Indy's own TThread objects directly, it can be done, depending on which version of Indy you are using:

    If you are using Indy 9 and earlier, the server events provide a TIdPeerThread object pointer. TIdPeerThread is a TThread descendant. Simple enough.

    If you are using Indy 10, on the other hand, it goes out of its way to hide/abstract the threads away from you. The server events now provide a TIdContext object pointer, and TIdContext is not the thread itself. But all is not lost. If the TIdTCPServer.Scheduler property is pointing at a TIdSchedulerOfThread-derived component (TIdTCPServer uses TIdSchedulerOfThreadDefault internally if you do not provide a Scheduler), then you can type-cast the TIdContext.Yarn property to a TIdYarnOfThread object pointer and then access the TIdYarnOfthread.Thread property, which is a TIdThreadWithTask object pointer. TIdThreadWithTask is a TThread descendant.