Le Café Central de Deva
..... Deva blogs!!
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:
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.
This isn't completely true in my experience. The threading model of outlook is ApartmentThreaded, and as long as you marshal your interfaces to other threads, and you are VERY careful to make calls that will run very quickly, it will work fine, right? Well, not completely. Outlook not only can't handle multiple threads (thus STA), it also can become overloaded with events from the COM calls. This is especially true when Outlook is sending receiving email. If you are calling Outlook from a background thread, it is sending WM_ events to the message pump to run. Then, sending/receiving email is causing all kinds of other events to call. Now, start doing things in Outlook while both of these are occuring, such as sending email. When you close the window, POOF. Outlook will illegal operation. If you are lucky enough to catch the stack at various points before it crashes, you'll see it hopping from one event to another without ever unwinding back to the main thread. Outlook has issues handling events. So if you want to write a plugin that you know will work? Keep on the main thread. If you want to write something to run in the background, you had better be an expert at timers and state machines.
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.