在Delphi线程中使用CoInitialize [英] Using CoInitialize in a Delphi thread

查看:30
本文介绍了在Delphi线程中使用CoInitialize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi程序的线程内使用TIdHttp和TXMLDocument.现在我想知道:

I am using TIdHttp and TXMLDocument inside a thread in a Delphi program. Now I want to know:

  1. 这些类是否使用COM对象,所以我需要在此线程中调用CoInitialize和CoUninitialize?
  2. 如果是,我是否必须在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 and CoUninitialize must be called once from within the thread context. Typically in the Execute() method of TThread, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆