SohoNYC75
SohoNYC75

Reputation: 21

VSTO Outlook Multithreading

I'm working on a VSTO Addin for Outlook and using .Net TPL/PFX Library to access the OOM.

Here's a snippet:

Parallel.ForEach(mailItem.Recipients.OfType<Outlook.Recipient>(), x =>                    
{
    try
    {
        Outlook.Recipient recipient = x as Outlook.Recipient;
        ...

I've come across some threads here talking about OOM running in STA so wondering if the above is useful or in anyway degrades performance. Also, what type of Cleanup/ComRelease should be accounted for in such code?

Can any VSTO Outlook expert comment on this please?

Upvotes: 1

Views: 979

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49435

Outlook uses the single-threaded apartment model, so you should deal with it on the main thread only. If latest Outlook versions detect cross-threaded calls you may get an exception as a result of such operations. I'd suggest gathering the required information from the OOM on the main (UI) thread and then process it using secondary threads, for example, if you need to make some web calls.

Also you may consider using a low-level API on which Outlook is based on - Extended MAPI. It is allowed to use that API on secondary threads. For example, you may consider using third-party libraries that were built on top of Extended MAPI and support using it in multithreading environments, the most famous one is Redemption.

Upvotes: 2

Related Questions