你能解释一下STA和MTA?你能、STA、MTA

2023-09-02 01:16:52 作者:没什么比敷衍更让人揪心

您可以用自己的语言解释一下STA和MTA?

Can you explain STA and MTA in your own words?

此外,什么是公寓线程和他们做只涉及到COM?如果是这样,为什么?

Also, what are apartment threads and do they pertain only to COM? If so, why?

推荐答案

COM线程模型被称为公寓模式,即初始化COM对象的执行上下文与任何单个线程(单线程公寓)相关或多线程(多线程公寓)。在该模型中,一个COM对象,一旦在公寓初始化,是公寓为它的持续时间的一部分的运行时间。

The COM threading model is called an "apartment" model, where the execution context of initialized COM objects is associated with either a single thread (Single Thread Apartment) or many threads (Multi Thread Apartment). In this model, a COM object, once initialized in an apartment, is part of that apartment for the duration of it's runtime.

该STA模型用于非线程安全的COM对象。这意味着他们不处理自己的同步。一个常见的​​用途是UI组件。因此,如果另一线程需要与对象进行交互(例如,在一个形式按下一个按钮),则该消息被编组到STA线程。 Windows窗体消息泵系统是一个这样的例子。

The STA model is used for COM objects that are not thread safe. That means they do not handle their own synchronization. A common use of this is a UI component. So if another thread needs to interact with the object (such as pushing a button in a form) then the message is marshalled onto the STA thread. The windows forms message pumping system is an example of this.

如果COM对象可处理其自己的同步然后MTA模型可以使用,其中多个线程被允许与对象进行交互,而整理的呼叫。

If the COM object can handle its own synchronization then the MTA model can be used where multiple threads are allowed to interact with the object without marshalled calls.