本文介绍了它需要编组com对象支持线程模型“两者”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在in-proc-server(.dll)中使用线程模型Both。注意到注册表编辑器中的线程模型。我们知道用Both创建的com对象将采用线程的线程模型其中创建对象。我在STA线程中实例化对象,以确保调用com对象应该只通过一个线程。我是从子线程以一种序列化的方式调用这个对象的方法,其中对象实际上是在主(STA)线程中创建



会产生任何副作用吗? p>

我没有得到任何错误,执行得很好,当我使用子线程中的对象没有任何编组(GIT)。



现在我的疑问是怎么发生的。没有得到任何staright方法回答这个当我google。请给我一个简短的描述abt这



根据我的理解编组对象是require如果对象支持STA是这样?这里我们的对象支持Both。

解决方案

请参阅。是否使用编组将取决于对象和呼叫者是否在同一公寓。如果我得到你的话正确,你调用 CoInitializeEx()将线程放入STA,然后你调用 CoCreateInstance()从该线程 - 对象将在相同的STA中创建,因此原始创建者线程将直接与其通信。其他线程将如何使用该对象将取决于公寓配置。从你的问题不清楚其他线程调用 CoInitializeEx()以及如何将对象指针传递给它。如果你只是传递一个原始的指针,那么没有办法如何编组会打开。



当你传递一个指针到另一个线程,你可能会遇到真正的问题。 说您应该永远不要在公寓之间传递原始指针。但是看起来像你的情况是该规则的例外。 COM类标记为具有 Both 线程模型,因此它必须是完全线程安全的,因此它的方法可以同时从多个线程调用。我不知道这个,但看起来像你是安全的。


We are given in-proc-server (.dll) with threading model "Both".i noticed the threading model in registry editor.as we know com object created with "Both" will take the threading model of the thread in which the object is created. i instantiated the object in a STA thread to make sure call to com object should go through only one thread. i was invoking the methods of this objects from child thread in a serialized way where object is actually created in main(STA) thread

will it make any side effects?

I did n't get any errors and executes fine when i used the object in child thread without any marshalling(GIT) .

now my doubt is how come it is happening.i didn't get any staright way answer for this when i google. please give me a brief description abt this

as per my understanding marshalling of an object is require if the object supports "STA" is it so? here our object supports "Both".

解决方案

See this very good explanation. Whether marshalling will be used will depend on whether the object and the caller are in the same apartment. If I get your words right you call CoInitializeEx() to place a thread into STA, then you call CoCreateInstance() from that thread - the object will be created in the same STA, so the original creator thread will communicate with it directly. How the other thread will use that object will depend on apartment configuration. It's unclear from your question whether the other thread calls CoInitializeEx() and how the object pointer is thransferred to it. If you just pass a raw pointer then there're no means for how marshalling would turn on.

When you pass a pointer to another thread you might encounter real problems. Part two of the above article says that you should never pass raw pointers between apartments. However looks like your case is an exception to that rule. The COM class is marked to have Both threading model, so it must be fully thread-safe and therefore its methods can be called from multiple threads simultaneously. I'm not sure about this, but looks like you're safe.

这篇关于它需要编组com对象支持线程模型“两者”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 00:49