Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. COM Single-Threaded Or Multi-Threaded

COM Single-Threaded Or Multi-Threaded

Scheduled Pinned Locked Moved C / C++ / MFC
comquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yccheok
    wrote on last edited by
    #1

    Hi, currently, I have an activeX control which will be created by one thread, and control will be used by many other thread. I only wish ONE thread to access the same method at ONE time. What If 1. My control already have a synchronized mechanism handling, i.e. each method of the control is protected by CRITICAL_SECTION. May I know which thread model i should use? CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) - single thread or CoInitializeEx(NULL, CO_MULTITHREADED) - multi thread 2. My control method is not protected by any CRITICAL_SECTION. again, what thread model i should use? thanks! cheok

    C 1 Reply Last reply
    0
    • Y yccheok

      Hi, currently, I have an activeX control which will be created by one thread, and control will be used by many other thread. I only wish ONE thread to access the same method at ONE time. What If 1. My control already have a synchronized mechanism handling, i.e. each method of the control is protected by CRITICAL_SECTION. May I know which thread model i should use? CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) - single thread or CoInitializeEx(NULL, CO_MULTITHREADED) - multi thread 2. My control method is not protected by any CRITICAL_SECTION. again, what thread model i should use? thanks! cheok

      C Offline
      C Offline
      Calc20
      wrote on last edited by
      #2

      If you don't have a sycronization mechanism in your control use COINIT_APARTMENTTHREADED (single threaded apartment) but make sure you call CoInitialize in each thread you want to access the control, and also you have to marshall object's interfaces to all threads from which you want to call the control, otherwise you'll get an exception. If you have a syncronization mechanism use the other model, multi threaded apartment and in this case you don't have to marshall interfaces.

      Y 1 Reply Last reply
      0
      • C Calc20

        If you don't have a sycronization mechanism in your control use COINIT_APARTMENTTHREADED (single threaded apartment) but make sure you call CoInitialize in each thread you want to access the control, and also you have to marshall object's interfaces to all threads from which you want to call the control, otherwise you'll get an exception. If you have a syncronization mechanism use the other model, multi threaded apartment and in this case you don't have to marshall interfaces.

        Y Offline
        Y Offline
        yccheok
        wrote on last edited by
        #3

        Can I please re-phrase your word? 1. For COINIT_APARTMENTTHREADED (single threaded apartment), I created a control in thread A (which had make a call to CoInitializeEx). If there is another thread B wish to use the control, thread B had to make call to CoInitializeEx/ CoUninitialize before/ after using the control. However, in multi thread apartment, I just need to call CoInitializeEx ONCE in thread A. thread B need NOT to call CoInitializeEx. 2. May I know what is mean by "marshall interfaces" ? Thanks

        C 1 Reply Last reply
        0
        • Y yccheok

          Can I please re-phrase your word? 1. For COINIT_APARTMENTTHREADED (single threaded apartment), I created a control in thread A (which had make a call to CoInitializeEx). If there is another thread B wish to use the control, thread B had to make call to CoInitializeEx/ CoUninitialize before/ after using the control. However, in multi thread apartment, I just need to call CoInitializeEx ONCE in thread A. thread B need NOT to call CoInitializeEx. 2. May I know what is mean by "marshall interfaces" ? Thanks

          C Offline
          C Offline
          Calc20
          wrote on last edited by
          #4

          Yes something like that, but in single threaded apartment model other threads have to call CoInitialize before using the control, not after. In multi threaded apartment although it's not necessary for each thread to call CoInitialize, it's recommended to make that call, to avoid problems, like access violations. If you access a COM interface from another apartment you'll get an exception, so when you are using single threaded apaprment model you have to marshall interfaces between threads. You do that by calling CoMarshalInterThreadInterfaceInStream() from the thread in which you called first CoInitialize(), and pass the IStream interface you get to the other thread, from which you want to access the object. In that thread call CoGetInterfaceAndReleaseStream() passing the IStream as argument and you'll get the interface. The easiest way is to marshall the IDispatch interface, because after that you can call directly any methods you want.

          Y 1 Reply Last reply
          0
          • C Calc20

            Yes something like that, but in single threaded apartment model other threads have to call CoInitialize before using the control, not after. In multi threaded apartment although it's not necessary for each thread to call CoInitialize, it's recommended to make that call, to avoid problems, like access violations. If you access a COM interface from another apartment you'll get an exception, so when you are using single threaded apaprment model you have to marshall interfaces between threads. You do that by calling CoMarshalInterThreadInterfaceInStream() from the thread in which you called first CoInitialize(), and pass the IStream interface you get to the other thread, from which you want to access the object. In that thread call CoGetInterfaceAndReleaseStream() passing the IStream as argument and you'll get the interface. The easiest way is to marshall the IDispatch interface, because after that you can call directly any methods you want.

            Y Offline
            Y Offline
            yccheok
            wrote on last edited by
            #5

            marshall interfaces seems not easy :( i think i will go directly for multithread apartment to avoid using marshall interfaces. since providing a synchronized protection on my activex control object much easier. :) thanks for help. this solve my doubt.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups