Multi threaded Addin

A

Ashok K Kumar

Hi all,

Is it alright to write multithreaded addins to Office application? If so,
then what threading model should the threads use (apartment or free)? What is
the best way to access the office objects from the new thread?

I recently wrote an Addin to word application that required to some
operation to be done on a specified timer interval. So I created an object of
the GIT (GlobalInterfaceTable) and registered the Application object
(Word::_Application), created the thread and passed the cookie that got from
the GIT. In the thread worker function, CoInitialized to
COINIT_APARTMENTTHREADED, created the GIT object and used the cookie passed
to get the application object. Everything works fine with the interface
pointer that I got, except when the thread cleans up. When I wanted the
thread to clean up and terminate, I did Application interface->Release() (the
one I got from the GIT). The Thread freezes. If I comment this line, the
CoUninitialize() freezes. First I used the CComPtr, the thread never
terminated and when I examined the thread stack by pausing, the thread frooze
in some call deep under NtDll.dll while trying to Release the Application
Object.

Can some one please explain what could be the problem?

Thanks

Ashok Kumar K
 
P

Peter Huang

Hi

What is the other thread than the main thread doing?
Will the other thread has something operation concerned about the main
thread's object?

You may take a look at link below.
Threading Support in Office
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/h
tml/wrconthreadingsupportinoffice.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
V

Vladimir Chtchetkine

AFAIK you can't touch Office interfaces (such as _Application) outside UI
thread. Although I didn't try this in Word, I tried it in Outlook. Here is
my approach:
1. In OnConnection (or OnStartupComplete) create a hidden window and save
its HWND.
2. Create your thread (in this thread I init COM with CoInitialize(NULL) and
don't have any problems with that)
3. When (in this thread) you need to access Word's API (such as
_Application) you don't do this directly in that thread. Instead you send
(or post - it depends on your model) a custom (WM_USED+xxx) message to the
window you created at step 1 with params indicating details of the operation
and perform that operation in the appropriate message handler. In this case
everything should work fine.
 

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