access the list of words of a word document from multiple threads
I recently had some problems with the performance of the Word object model. In an add-in that I wrote for Word I need to parse through all the words of a document and replace some of them or ask the user for the ones that have multiple replacements. I know that it is faster to ask Word for all of the document text content at once and then process it and put it back all at once again, but this is not suitable for my add-in because I need to have access to the range objects that represent the words that have multiple replacements so that I can somehow mark them in the document and present the user with a tool tip from which he can select the replacement he wants.
So for the moment the single great speed improveme开发者_StackOverflownt that came in my head was multithreading since most people already have dual core or better. The problem is that all the things you find on Google say that multithreading in Office is a very bad thing to do.
So is there any one who managed to do this in a manner that worked in most of its usage? By this I mean if it also worked on other PCs then the development one?
And a second Q is: Does anyone know why Microsoft restricted the Word ( Office) object model to single thread? Just out of curiosity :)
Multithreading in Office should work just fine. Here's an article from Microsoft on doing just that:
Walkthrough: Multithreading with the BackgroundWorker Component (C# and Visual Basic)
The Office object model isn't single threaded but rather Single Thread Apartment (STA). This is actually the default state for all VB.Net programs (C# is MTA by default). To learn more about STA vs MTA check out these links:
- Could you explain STA and MTA?
- difference between STA and MTA (check out the third post and on)
精彩评论