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. Is that all there is to it?
For 2.) Consider the example of the Form having a textbox and both the Thread created by the Form and also a Task created by this Thread both needing to update the text. I initially tried the textbox.InvokeRequired/Invoke pattern to pass the update to the UI thread but this froze Outlook when done from a Task which was not STA. But the Thread could update the textbox just fine. To fix it so the non-STA Task could update the textbox without freezing, I simply changed Invoke to BeginInvoke. Since Invoke is blocking and BeginInvoke is nonblocking it would seem to point to some type of deadlock, but I fail to understand why either an STA thread or a thread spawned from the UI thread could work with both Invoke and BeginInvoke. So bottom line, what is the best practice to do 2.) without freezing or deadlocking Office?
Aucun commentaire:
Enregistrer un commentaire