ADOQuery in threads...

I tried to use a TADOQuery (delphi 5) object in a TThread. On the TADOQuery.Create, an exception was raised that 'CoInitialize' had not been called. So I simple called ole2.CoInitialize(nil), and without knowing what truly happens, everything seems to work. Even if I launch 50 threads and let them query an access database, all results seems to be allright.

- Is this ok, or just a dirty workaround?
- the delphi help states that bde access connection should not be used in threads, since the ADO library would not be thread safe. Is this still true, and so applicable to the new ADO components as well?
- Performance within the threads seems to be a little slow. Is this due to the simulanious reads of the access database, or are the ADO calls not handled in parallel?
Who is Participating?
john_mConnect With a Mentor Commented:
You are correct in calling CoInitialize in the thread. Before any thread other than the main VCL thread makes any calls to the COM sub system, that thread must be initialised to use the system. Since ADO is inherently a member of the COM subsystem you must call CoInitialise. I haven't used the Delphi 5 ADO components, unfortunately they weren't around 12 months ago when I started working with ADO, however I'd assume that you would also have to call CoUninitialize before your thread terminates. In the words of Microsoft CoUninitialize....

"Closes the COM library on the current thread, unloads all DLLs loaded by the thread, frees any other resources that the thread maintains, and forces all RPC connections on the thread to close."

Hope that this helps your understanding,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.