是否可以使两个COM STA实例具有相同的组件? [英] Is possible having two COM STA instances of the same component?

查看:106
本文介绍了是否可以使两个COM STA实例具有相同的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在这里的另一个线程上发现 ,我需要访问一个STA的COM组件.我将在双核计算机上运行它,使用此组件的进程只能达到50%的CPU.不幸的是,所有者表示他们无法将组件更改为MTA,因为该组件是在Matlab上编译的混合系统,其核心是C.

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.

因此,我尝试在同一进程上加载COM类的两个实例,不同的线程访问它,但是我不能,只有最后一个COM实例可用.你知道什么可以解决这个问题吗?

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?

我正在考虑在同一台计算机上运行我的服务的两个进程,以实现100%cpu.这不是一个好的解决方案,主要是因为此服务器将安装在我们的基础设施之外.

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.

推荐答案

关于多个STA组件的主题

可能具有相同STA COM组件的两个实例,并可以从C#访问它们.唯一可能阻止您出现这种情况的是,如果将对象实现为单例对象,则该对象本身.

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.

但是,如果两个实例都在同一STA线程上,则其中一个实例中的活动调用将阻止对该线程的任何其他调用.因此,如果您希望这两个实例并行工作,则需要将它们放在单独的STA线程上.为了安全起见,我将在后台线程上创建两个实例.那应该可以防止UI锁定.

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.

关于外部组件的STA与MTA的话题

我不确定C中的组件为什么会阻止它成为MTA对象.成为MTA只是意味着对象需要在多个线程之间内部同步其状态访问和管理代码.

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.

警告:丑陋的黑客! :-)如果您想尝试一下,可以转到注册表并将外部组件线程模型从Apartment更改为Free,只是为了验证您的代码可以与MTA一起正常使用.不过,由于它们可能没有编写线程安全的代码,而是依靠COM来保护它们,因此它们的组件可能会损坏.

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. :-)

这篇关于是否可以使两个COM STA实例具有相同的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆