在Delphi线程中使用CoInitialize [英] Using CoInitialize in a Delphi thread
问题描述
我在Delphi程序的线程内使用TIdHttp和TXMLDocument.现在我想知道:
I am using TIdHttp and TXMLDocument inside a thread in a Delphi program. Now I want to know:
- 这些类是否使用COM对象,所以我需要在此线程中调用CoInitialize和CoUninitialize?
- 如果是,我是否必须在execute方法的主体或使用TIdHttp或TXMLDocument类的所有方法上使用这些功能?
推荐答案
-
TIdHTTP
没有COM依赖项.TXMLDocument 可以依赖于COM.在Windows上,开箱即用是Microsoft的MSXML ActiveX组件的包装,该组件使用COM.如果使用其他DOM供应商(例如,XE7上的OmniXML),则没有COM依赖项.您可以通过设置
DefaultDOMVendor
来控制此操作>全局变量.TXMLDocument can have a dependency on COM. On Windows, out of the box it is a wrapper around Microsoft's MSXML ActiveX component, which uses COM. If you use another DOM vendor (for example, OmniXML, available from XE7) then there is no COM dependency. You can control this by setting the
DefaultDOMVendor
global variable.CoInitialize
和CoUninitialize
.通常在TThread
的Execute()
方法中,如本示例流程所示:CoInitialize
andCoUninitialize
must be called once from within the thread context. Typically in theExecute()
method ofTThread
, as seen in this example flow:procedure TMyThread.Execute; begin try CoInitialize(nil); try while not Terminated do begin DoWorkThatMayUseCOM; end; finally CoUninitialize(); end; except on E: Exception do // log exception Log(E); end; end;
这篇关于在Delphi线程中使用CoInitialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!