none
Weird problem in multithreaded VSTO Word add-in RRS feed

  • Question

  • Hi,
    I'm working on a Word add-in which basically gets data from a WCF service and displays it to the user in Word. Because the amount of data and bandwidth can vary wildly I have to keep the user entertained somehow as opposed to just letting Word hang. Therefore, I delegated the download task to a BackgroundWorker.  A simple modal window which shows a progress bar pops up after the BackgroundWorker begins work.  
    This is where things get weird. The download task includes accessing the Word document's CustomXML before calling the WCF service. When delegated to the background worker this operation fails with this exception:
    System.Runtime.InteropServices.COMException (0x8002801D): Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))
       at Microsoft.Office.Interop.Word.DocumentClass.get_CustomXMLParts()
    Searching for the issue yielded only trivial and unhelpful answers such as reinstalling PIA/VSTO/VS/Office. 
    Here's a piece of code from the add-in:
    #if nosplash
                        result = Download(Application.ActiveDocument);
    #else
                        SplashTask dlg = (SplashTask)delegate
                        { return Download(Application.ActiveDocument);};
                        result = (bool)ShowSplash(dlg);
    #endif
    If nosplash is defined, everything works smoothly, without any exception. If nosplash is not defined then I get the aforesaid exception. Please help.
    Wednesday, October 26, 2011 5:03 PM

Answers

  • They were simply not designed to be accessed from multiple threads.

    Even though it may sometimes work, your code can and will fail at the most inappropriate moment.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    • Marked as answer by _Al3x_ Wednesday, October 26, 2011 6:53 PM
    Wednesday, October 26, 2011 6:04 PM

All replies

  • Office objects can only be accessed from the main thread.

    Why not read the XML on the main thread before starting the thread?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Wednesday, October 26, 2011 5:28 PM
  • Hmm. I actually thought about this workaround but is there some specific reason for not allowing multithreaded access to Office objects?
    Wednesday, October 26, 2011 5:49 PM
  • They were simply not designed to be accessed from multiple threads.

    Even though it may sometimes work, your code can and will fail at the most inappropriate moment.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    • Marked as answer by _Al3x_ Wednesday, October 26, 2011 6:53 PM
    Wednesday, October 26, 2011 6:04 PM
  • Ok, I see. Then I guess it's problem solved. Thanks.
    Wednesday, October 26, 2011 6:53 PM