• <bdo id='IvYeO'></bdo><ul id='IvYeO'></ul>
  • <tfoot id='IvYeO'></tfoot>

    <small id='IvYeO'></small><noframes id='IvYeO'>

      <i id='IvYeO'><tr id='IvYeO'><dt id='IvYeO'><q id='IvYeO'><span id='IvYeO'><b id='IvYeO'><form id='IvYeO'><ins id='IvYeO'></ins><ul id='IvYeO'></ul><sub id='IvYeO'></sub></form><legend id='IvYeO'></legend><bdo id='IvYeO'><pre id='IvYeO'><center id='IvYeO'></center></pre></bdo></b><th id='IvYeO'></th></span></q></dt></tr></i><div id='IvYeO'><tfoot id='IvYeO'></tfoot><dl id='IvYeO'><fieldset id='IvYeO'></fieldset></dl></div>

    1. <legend id='IvYeO'><style id='IvYeO'><dir id='IvYeO'><q id='IvYeO'></q></dir></style></legend>

        COM 多线程支持

        时间:2023-07-26
        <i id='0r8mf'><tr id='0r8mf'><dt id='0r8mf'><q id='0r8mf'><span id='0r8mf'><b id='0r8mf'><form id='0r8mf'><ins id='0r8mf'></ins><ul id='0r8mf'></ul><sub id='0r8mf'></sub></form><legend id='0r8mf'></legend><bdo id='0r8mf'><pre id='0r8mf'><center id='0r8mf'></center></pre></bdo></b><th id='0r8mf'></th></span></q></dt></tr></i><div id='0r8mf'><tfoot id='0r8mf'></tfoot><dl id='0r8mf'><fieldset id='0r8mf'></fieldset></dl></div>

          <bdo id='0r8mf'></bdo><ul id='0r8mf'></ul>
                <tbody id='0r8mf'></tbody>
            1. <legend id='0r8mf'><style id='0r8mf'><dir id='0r8mf'><q id='0r8mf'></q></dir></style></legend>

            2. <small id='0r8mf'></small><noframes id='0r8mf'>

                  <tfoot id='0r8mf'></tfoot>
                  本文介绍了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 多线程支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用C#在Windows中的任务栏上放置一个控件 下一篇:如何获取打开的资源管理器窗口的 PIDL?

                  相关文章

                  1. <i id='VYmiz'><tr id='VYmiz'><dt id='VYmiz'><q id='VYmiz'><span id='VYmiz'><b id='VYmiz'><form id='VYmiz'><ins id='VYmiz'></ins><ul id='VYmiz'></ul><sub id='VYmiz'></sub></form><legend id='VYmiz'></legend><bdo id='VYmiz'><pre id='VYmiz'><center id='VYmiz'></center></pre></bdo></b><th id='VYmiz'></th></span></q></dt></tr></i><div id='VYmiz'><tfoot id='VYmiz'></tfoot><dl id='VYmiz'><fieldset id='VYmiz'></fieldset></dl></div>

                  2. <small id='VYmiz'></small><noframes id='VYmiz'>

                  3. <tfoot id='VYmiz'></tfoot>

                  4. <legend id='VYmiz'><style id='VYmiz'><dir id='VYmiz'><q id='VYmiz'></q></dir></style></legend>

                      <bdo id='VYmiz'></bdo><ul id='VYmiz'></ul>