Why are some objects not accessible from different threads?

后端 未结 1 1324
猫巷女王i
猫巷女王i 2021-02-19 09:26

I\'ve come across this problem several times while developing in C#. I\'ll be a happily coding along, passing objects to and fro between threads and what not, then all of a sud

相关标签:
1条回答
  • 2021-02-19 09:56

    I think this may explain things fairly well, I think this specifically has to do with COM.

    http://msdn.microsoft.com/en-us/library/ms693344%28v=vs.85%29

    specifically.

    In general, the simplest way to view the COM threading architecture is to think of all the COM objects in the process as divided into groups called apartments. A COM object lives in exactly one apartment, in the sense that its methods can legally be directly called only by a thread that belongs to that apartment. Any other thread that wants to call the object must go through a proxy.

    There are two types of apartments: single-threaded apartments, and multithreaded apartments.

    Single-threaded apartments consist of exactly one thread, so all COM objects that live in a single-threaded apartment can receive method calls only from the one thread that belongs to that apartment. All method calls to a COM object in a single-threaded apartment are synchronized with the windows message queue for the single-threaded apartment's thread. A process with a single thread of execution is simply a special case of this model.

    Multithreaded apartments consist of one or more threads, so all COM objects that live in an multithreaded apartment can receive method calls directly from any of the threads that belong to the multithreaded apartment. Threads in a multithreaded apartment use a model called free-threading. Calls to COM objects in a multithreaded apartment are synchronized by the objects themselves.

    0 讨论(0)
提交回复
热议问题