简体   繁体   English

在Delphi线程中使用CoInitialize

[英]Using CoInitialize in a Delphi thread

I am using TIdHttp and TXMLDocument inside a thread in a Delphi program. 我在Delphi程序的线程内使用TIdHttp和TXMLDocument。 Now I want to know: 现在我想知道:

  1. Do these classes use COM objects so I need to call CoInitialize and CoUninitialize in this thread? 这些类是否使用COM对象,所以我需要在此线程中调用CoInitialize和CoUninitialize?
  2. If yes, do I have to use these functions at the body of execute method or at all methods that use TIdHttp or TXMLDocument classes? 如果是,我是否必须在execute方法的主体或使用TIdHttp或TXMLDocument类的所有方法上使用这些功能?
  • TIdHTTP has no COM dependency. TIdHTTP没有COM依赖性。

  • TXMLDocument can have a dependency on COM. TXMLDocument 可以依赖于COM。 On Windows, out of the box it is a wrapper around Microsoft's MSXML ActiveX component, which uses COM. 在Windows上,它是Microsoft MSXML ActiveX组件的包装,该组件使用COM。 If you use another DOM vendor (for example, OmniXML, available from XE7) then there is no COM dependency. 如果使用其他DOM供应商(例如,XE7的OmniXML),则没有COM依赖性。 You can control this by setting the DefaultDOMVendor global variable. 您可以通过设置DefaultDOMVendor全局变量来控制它。

  • CoInitialize and CoUninitialize must be called once from within the thread context. 必须从线程上下文中一次调用CoInitializeCoUninitialize Typically in the Execute() method of TThread , as seen in this example flow: 通常在TThreadExecute()方法中,如本示例流程所示:

     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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM