Search code examples
delphiindy

Delphi (Indy + ADO) CoInitialize Slow


I am trying to get each connection thread in a TIdTCPServer instance to instanciate a TADOConnection into the context.

This works fine, and I place the CoInitialize(nil) in the OnConnect event handler. Problem is it takes around 20-25 seconds for this process to work, which is WAY to long...

Any advice?

Cheers, Adrian


Solution

  • It is very unlikely that either CoInitialize() or ADO takes that long to initialize. I have worked with multithreaded COM for years and never experienced such a delay.

    That being said, a better place to initialize COM is at the top of the calling thread's Execute() method rather than in the server's OnConnect event. To do that with Indy, derive a new class from TIdThreadWithTask and override its BeforeExecute() and AfterExecute() methods. Then instantiate a TIdSchedulerOfThread... component, assign your class to its ThreadClass property, and assign it to the server's Scheduler property before activating the server.