COM 多线程支持

COM multi-threading support(COM 多线程支持)

本文介绍了COM 多线程支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次使用 COM我有这个 COM dll,比如 ABCServer.dll,我创建了一个 RCW 并在我的项目中添加了对它的引用.现在我的应用程序创建了几个线程,每个线程从 COM dll 创建某些类并使用它们.但随后每个线程都在等待,而其他线程正在使用 COM dll 中的某个类.

Working with COM for the first time I have got this COM dll, say ABCServer.dll, i created a RCW and added reference to it in my project. Now my application creates several threads and each thread creates certain classes from COM dll and works with them. But then each thread is waiting while other thread is working with certain class from COM dll.

修改我的应用程序的全部目的是在其上启用多线程.现在,当我身边发生多线程时,COM 导致它是顺序的.虽然每个线程都在创建新实例,但为什么还要等待其他线程被处理?

The whole purpose of modifying my application was enabling multi-threading on it. Now when multithreading is happening at my side, the COM causing it to be sequential. Although each thread is creating new instances, why are they waiting for others to be processed?

推荐答案

到目前为止发布的评论和答案存在相当大的混乱.STA 或 MTA 是线程的属性.但重要的是 COM 组件需要什么.这是在 ThreadingModel 注册表值中声明的.您找到了公寓",这是一个非常常见的设置.这意味着该组件不支持线程.

There's considerable confusion in the comments and answers posted so far. STA or MTA is a property of a thread. But what matters is what the COM component requires. That's declared in the ThreadingModel registry value. You found "Apartment", a very common setting. It means that the component does not support threading.

这与 .NET 框架中的绝大多数类没有什么不同,只有极少数是线程安全的.最大的区别在于 COM 强制线程安全.对于 .NET 类,您可以自行编写代码,以便以线程安全的方式使用类对象.这很难做到正确,并且是很难诊断错误的长期来源,但精心设计的锁定允许您绕过限制.这在 COM 是不可能的,它总是在没有你帮助的情况下提供线程安全.即使您确实想提供帮助.

This is not unlike the vast majority of classes in the .NET framework, there are very few that are thread-safe. The big difference is that COM enforces threading safety. With .NET classes it is up to you to write your code so the class objects are used in a thread-safe manner. That's difficult to get right and a chronic source of very hard to diagnose bugs but well designed locking allows you to sail around the restrictions. That's not possible with COM, it will always provide thread-safety without you helping. Even if you do want to help.

取得成功的唯一方法是仅从一个线程创建和使用 COM 组件.该线程必须使用 Thread.SetApartmentState() 初始化以选择 STA.这会阻止 COM 创建自己的线程来为组件找到安全的避风港.而且您必须抽出消息循环,这是 STA 要求.如果您不尝试从另一个线程使用 COM 组件并且组件本身不依赖于消息泵可用,这可能会很痛苦并且可能可以避免.顺便说一句,这很常见.当它停止响应时,您会注意到它需要一个,通常不会在预期时触发事件.仅从同一线程对 COM 对象进行调用,以与创建自己的对象的其他线程获得并发.这通常没有用或不可能.

The only way to get ahead is to create and use the COM component from only one thread. That thread must be initialized with Thread.SetApartmentState() to select STA. Which prevents COM from creating its own thread to find a safe haven for the component. And you must pump a message loop, an STA requirement. Which can be painful and might be avoided if you don't try to use the COM component from another thread and the component itself doesn't depend on having a message pump available. Which is pretty common btw. You'll notice it needs one when it stops being responsive, typically not firing an event when expected. Only make calls on the COM object from the same thread to get concurrency with other threads that create their own object. This is not typically useful or possible.

这篇关于COM 多线程支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:COM 多线程支持

基础教程推荐