从多个线程访问Word文档的单词列表
我最近在 Word 对象模型的性能方面遇到了一些问题。在我为 Word 编写的插件中,我需要解析文档的所有单词并替换其中的一些单词,或者询问用户有多个替换的单词。我知道一次向 Word 询问所有文档文本内容然后处理它并再次将其全部放回去会更快,但这不适合我的加载项,因为我需要访问该范围代表具有多个替换的单词的对象,以便我可以以某种方式在文档中标记它们并向用户提供工具提示,他可以从中选择他想要的替换。
因此,目前我想到的唯一一个巨大的速度改进是多线程,因为大多数人已经拥有双核或更好的处理器。问题是,你在 Google 上找到的所有内容都表明 Office 中的多线程是一件非常糟糕的事情。
那么是否有人能够以一种在大多数用途中都有效的方式做到这一点呢?我的意思是,如果它也适用于其他电脑,那么开发电脑呢?
第二个问题是:有谁知道为什么微软将Word(Office)对象模型限制为单线程?只是出于好奇:)
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 improvement 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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Office 中的多线程应该可以正常工作。下面是 Microsoft 的一篇关于执行此操作的文章:
演练:使用 BackgroundWorker 组件进行多线程处理 ( C# 和 Visual Basic)
Office 对象模型不是单线程的,而是单线程单元 (STA)。这实际上是所有 VB.Net 程序的默认状态(
C# 默认为 MTA)。要了解有关 STA 与 MTA 的更多信息,请查看以下链接: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: