CoInitialize error working with database inside threads

Using Delphi 7, whenever I try to do any database work inside a thread, I get this error:

"CoInitialize is not called"

I used simple code containing ADOConnection.Open inside the stream.

But does the same code work fine if it uses any ideas in the form?

+4
source share
3 answers

@mjn: I am not allowed to comment on your remark in the previous answer, so I created a new answer: calling CoInitialize from the constructor is one of the typical error programmers.

The constructor is executed in the context of another thread, but you need to initialize COM in the current thread (when the thread procedure is executed, i.e. as part of the Execute method), see

+14
source
procedure TYourThread.execute; begin CoInitialize(nil); FConnection:=TConnection.Create(...); try ThreadCode .... finally FConnection.free; CoUninitialize; end; end; 
+10
source

Another reason is because Application.Initialize; missing or commented on in the main DPR application.

0
source

All Articles