jeudi 2 avril 2015

VSTO with Windows Form and Worker Threads

I have an Office addin (Outlook if it makes a difference) created with VSTO. The addin creates a Windows Form at startup. My Form creates several threads. I'm looking for either concrete guidance or peoples' experiences with what safely works for the following situations:


1.) A thread created by the Form needs to access the Office object model (Globals.ThisAddIn.Application)


2.) A thread created by the Form does not need to access the Office object model, but does need to update a control on the Windows Form (the Form was created by the addin or 'UI' thread as I sometimes hear it referred to)


For 1.) above I've taken the following http://ift.tt/1BCTW0P to mean that it is safe as long as you set the thread's apartment state to STA and you handle exceptions. But http://ift.tt/19Ot9XX seems to imply that in .NET VSTO, calls to the object model from any background thread are safe as they're automatically marshalled to the main thread for you, and making the 'background' thread STA is merely for performance reasons. Is that all there is to it?


For 2.) Is there any problem with letting the 'thread' be a Task or a IsBackground thread, provided it uses the control's InvokeRequired/Invoke pattern? Or does it need to be a STA thread doing the Invoking?


Aucun commentaire:

Enregistrer un commentaire