Freigeben über


Development : Threading with Outlook Object Model?

Developers do complain when they use multi-threading the Outlook Object Model (OOM) API, it fails or hangs inside and outside of Outlook?  The reason behind this is the Outlook Object Model is not thread safe which means that COM will never allow multiple threads to enter concurrently.  Instead, it will serialize all calls using a message-based protocol which, in turn, means that the user will not be able to interact with Outlook while your background thread is being serviced and vice-versa.

If your COM object needs to believe that it is in a single-threaded environment, use STA. You are guaranteed that the creation and all calls will be made by the same thread. You can safely use Thread local storage and you don't need to use critical sections.

In general, rules for single-threaded apartments are simple, but it is important to follow them carefully:

  • Every object should live on only one thread (within a single-threaded apartment).
  • Initialize the COM library for each thread.
  • Marshal all pointers to objects when passing them between apartments.
  • Each single-threaded apartment must have a message loop to handle calls from other processes and apartments within the same process. Single-threaded apartments without objects (client only) also need a message loop to dispatch the broadcast messages that some applications use.
  • DLL-based or in-process objects do not call the COM initialization functions; instead, they register their threading model with the ThreadingModel named-value under the InprocServer32 key in the registry. Apartment-aware objects must also write DLL entry points carefully. There are special considerations that apply to threading in-process servers. For more information, see In-Process Server Threading Issues.

To isolate the issue, you can try running two instances of Outlook in same machine? Can you do that. The answer is No, you can’t do that. So all calls to the Outlook object model execute on Outlook’s main foreground thread. When you design or develop application, the only threading model supported by Outlook object model is single-threaded apartment (STA). Calling the Outlook object model from a background thread is not supported.

I recommend you also to refer pcreehan’s blogpost regarding this.

Comments

  • Anonymous
    November 09, 2010
    The comment has been removed
  • Anonymous
    November 10, 2010
    Thanks for your update, Brian, When i refer "When you design or develop application, the only threading model supported by Outlook object model is single-threaded apartment (STA). Calling the Outlook object model from a background thread is not supported" is stands of Microsoft CSS official support perspective. As i told you earlier you can try anything. I recommend you also to refer pcreehan’s blogpost (blogs.msdn.com/.../outlook-crashes-when-using-outlook-object-model-in-multiple-threads.aspx) regarding this. You can notice quite lot of relevant posts in Microsoft forums too.