How to access Outlook.MailItem from another thread?

P

pete

Hi,

I'm writing an Outlook addin in C# which modifies the attachments of an
email. I have hooked the ItemSend event on the application object, and
am spawning a new thread when an email is attempted to be sent. This
thread is passed the Outlook.MailItem object, but i've noticed that if
i access this object from the background thread then this can cause
issues.

If an Outlook email window is spawned using the "mailto:" command, the
addin is loaded etc but the OUTLOOK.EXE process doesn't terminate after
the email is sent and if I try to open outlook afterwards i get a
messagebox with the message "Operation failed". This is resolved by
terminating the rouge outlook process.

Through testing i've discovered this only happens when i touch the
Outlook.MailItem from the other thread. How can I safely access the
mailitem object from another thread?
 
K

Ken Slovak - [MVP - Outlook]

Outlook is not thread safe, don't spawn a thread like that (especially in an
event handler like ItemSend). Do your processing involving Outlook in the
main thread, let the event proceed and do any extra non-Outlook processing
in the background thread.
 
P

pete

I've spent the afternoon re-writing all the code to only access the
MailItem object in the main thread, and it works! Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top