none
Can't create a COM object in a worker thread RRS feed

  • Question

  • I create a worker thread in an MFC GUI application.In the thread I do CoInitialize and then try create a COM object.  The object's class is derived from CCmdTarget. The CreateInstance hangs until the main thread is busy doing some processing and comes back only after the main thread is idling. Any other processing goes in parallel in both threads with no problems. I don't have problems with creating other COM objects that are not derived from CCmdTarget.
     How can I create this COM object in the worker thread while the main thread is doing some processing as well? Thank you

    VXB

    Monday, July 22, 2013 12:57 PM

Answers

  • This may happen if the COM object you create is so-called legacy single-threaded - that is, it lacks ThreadingModel value under its HKCR\CLSID\{CLSID}\InprocServer32 registry key. Such objects are always created on the main STA thread - a thread that was the first to join a single-threaded apartment - which requires that this thread run its message pump.

    Make sure your COM object is registered with an appropriate threading model. Most likely, you want ThreadingModel=Apartment.


    Igor Tandetnik

    • Marked as answer by vxb Friday, July 26, 2013 2:16 PM
    Wednesday, July 24, 2013 4:59 PM

All replies

  • Hi,

    I am moving your thread into the Visual C++ Forum for dedicated support. Thanks for your understanding.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 24, 2013 6:58 AM
  • This may happen if the COM object you create is so-called legacy single-threaded - that is, it lacks ThreadingModel value under its HKCR\CLSID\{CLSID}\InprocServer32 registry key. Such objects are always created on the main STA thread - a thread that was the first to join a single-threaded apartment - which requires that this thread run its message pump.

    Make sure your COM object is registered with an appropriate threading model. Most likely, you want ThreadingModel=Apartment.


    Igor Tandetnik

    • Marked as answer by vxb Friday, July 26, 2013 2:16 PM
    Wednesday, July 24, 2013 4:59 PM
  • This may happen if the COM object you create is so-called legacy single-threaded - that is, it lacks ThreadingModel value under its HKCR\CLSID\{CLSID}\InprocServer32 registry key. Such objects are always created on the main STA thread - a thread that was the first to join a single-threaded apartment - which requires that this thread run its message pump.

    Make sure your COM object is registered with an appropriate threading model. Most likely, you want ThreadingModel=Apartment.


    Igor Tandetnik

    Hi vxb,

    AS Igor suggested, you should check your main thread threadingmodel, if it is STA(single-thread apartment), you should create the COM object in a STA thread. Make sure the COM object is registered with an appropriate threading model.

    What's more, I find below information helpful to  understand  the COM STA and MFC related threading model.  Hope we can learn more from it.

    http://www.codeproject.com/Articles/9190/Understanding-The-COM-Single-Threaded-Apartment-Pa  


    http://www.developer.com/net/cplus/article.php/3380661/MFC-and-NET-Why-ShellExecute-Isnt-Dependable-and-How-to-Work-Around-It.htm

    Best Regards!


    May Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 25, 2013 5:52 AM

  • Igor, Thank you so much - you were absolutely right. That was a very old COM component with no ThreadingModel key. I added that key and it works now. I really appreciate your help.

    Ogromnoe spasibo


    • Edited by vxb Friday, July 26, 2013 2:15 PM Wrong facts
    Friday, July 26, 2013 12:28 PM