同一组件是否可以有两个 COM STA 实例?
我在另一个线程上发现了一个问题,我需要访问作为 STA 的 COM 组件。 我将在双核计算机上运行它,使用该组件的进程仅达到 CPU 的 50%。 不幸的是,业主说他们不能将组件更改为MTA,因为该组件是在Matlab编译的混合系统,其核心是C。
所以我尝试在同一个进程上加载COM类的两个实例,不同的线程访问它,但我不能,只有最后一个 COM 实例变得可用。 你知道有什么可以解决这个问题吗?
我正在考虑在同一台计算机上运行我的服务的两个进程,以实现 100% cpu 利用率。 这不是一个好的解决方案,主要是因为该服务器将安装在我们的基础设施之外。
I had a problem discovered on another thread here, I need to access a COM component that is STA. I'll run it on a dual-core computer, a process using this component only reaches 50% of CPU. Unfortunately, the owners said they can't change the component to MTA, because the component is a hybrid system compiled at Matlab, which core is C.
So I tried to load two instances of the COM class on the same process, different threads accessing it, but I couldn't, only the last COM instance becomes usable. Do you know anything that could solve this problem?
I am considering running two processes of my service on the same computer, to achieve 100% cpu. This is not a good solution, mainly because this servers will be installed outside our infra.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于多个 STA 组件的主题
可以拥有同一 STA COM 组件的两个实例并从 C# 访问它们。 唯一可以防止这种情况发生的是对象本身(如果实现为单例对象)。
但是,如果两个实例位于同一 STA 线程上,则其中一个实例中的活动调用将阻止对该线程的任何其他调用。 因此,如果您希望这两个实例并行工作,则需要让它们位于单独的 STA 线程上。 为了安全起见,我会在后台线程上创建两个实例。 这应该可以防止您的 UI 锁定。
关于外部组件的 STA 与 MTA 的主题
我不确定为什么 C 中的组件会阻止它成为 MTA 对象。 MTA 只是意味着对象需要在多个线程之间内部同步其状态访问和管理代码。
警告:丑陋的黑客! :-) 如果您想尝试一下,您可以转到注册表并将外部组件线程模型从 Apartment 更改为 Free,只是为了验证您的代码是否可以正常工作与 MTA。 不过,他们的组件可能会崩溃,因为他们可能没有编写线程安全代码,而是依靠 COM 来保护它们。
在显眼的地方做个记号,以便稍后恢复该更改,这样您就不会得到一个代码无法工作的系统,并花费无数时间追赶幽灵。 :-)
On the topic of multiple STA components
It is possible to have two instances of the same STA COM component and access them from C#. The only thing that could prevent you from such scenario is the object itself if implemented as a singleton object.
However, if both instances are on the same STA thread, an active call in one of the instances will block any other calls into that thread. Thus, if you want those two instances to work in parallel, you need to have them be on separate STA threads. Just to be on the safe side, I'd create both instances on background threads. That should prevent your UI from locking.
On the topic of STA vs. MTA for the external component
I am not sure why the component being in C would prevent it from being an MTA object. Being MTA just means the object needs to internally synchronize it's state access and management code between multiple threads.
WARNING: Ugly hack! :-) If you want to experiment a bit, you could go to the registry and change the external component threading model from Apartment to Free, just to verify that your code would work properly with an MTA. Their component will probably break, though, as they probably did not write thread-safe code, relying on COM to guard them.
Make a note on a prominent place to revert that change later, so that you don't end up with a system where their code doesn't work and spent countless hours chasing ghosts. :-)
Franci Pernov,
我尝试使用两个线程,并在每个线程的上下文中初始化 com 实例,但错误是相同的:(来自 HRESULT 的异常:0x80004005 (E_FAIL))
我通过 CallContext 存储和检索实例获取数据和设置数据。
Franci Pernov,
I've tried work with two threads, and initialize the com instances on the context of each thread, but the error is the same: (Exception from HRESULT: 0x80004005 (E_FAIL))
I am storing and retrieving the instance through CallContext GetData and SetData.
尝试使用相同的 DLL 注册第二个类。 请考虑,为了完全安全,您实际上可能需要具有不同名称的 DLL 的单独副本。
请记住,STA COM 类(可能还有它的 DLL)对于多线程来说不被认为是线程安全的,并且对于 COM 类的外部您无能为力。
Try registering a second class using the same DLL. Consider that you may actually need a separate copy of the DLL with a different name in order to be completely safe.
Just remember that the STA COM class (and perhaps its DLL) is not considered thread safe for multi-threading and there is nothing you can do about that external to the COM class.