Using CoInitialize in a Delphi thread

半世苍凉 提交于 2021-01-27 23:16:19

问题


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

  1. Do these classes use COM objects so I need to call CoInitialize and CoUninitialize in this thread?
  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?

回答1:


  • TIdHTTP has no COM dependency.

  • 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 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;
    


来源:https://stackoverflow.com/questions/42904464/using-coinitialize-in-a-delphi-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!