C# 单线程单元 vs 多线程单元

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/485086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:14:03  来源:igfitidea点击:

Single-Threaded Apartments vs Multi-Threaded Apartments

c#.netmultithreadingapartments

提问by Anthony D

Possible Duplicate:
Could you explain STA and MTA?

可能重复:
您能解释一下 STA 和 MTA 吗?

All ThreadPool threads are in the multithreaded apartment.

所有 ThreadPool 线程都在多线程单元中。

--As per the MSDN

--根据MSDN

What does that mean? I am really concerned with what the difference between the multi vs single threaded apartment model is. Or what does the apartment model mean? I have read the MSDN on it, and it doesn't really make sense to me. I think I may have an idea, but I was thinking someone on here could explain it in plain English.

这意味着什么?我真的很关心多线程与单线程公寓模型之间的区别。或者公寓模型是什么意思?我已经阅读了关于它的 MSDN,它对我来说没有意义。我想我可能有一个想法,但我想这里有人可以用简单的英语解释它。

Thanks, Anthony D

谢谢,安东尼 D

Update 1

更新 1

Found this Could you explain STA and MTA?

找到这个 你能解释一下 STA 和 MTA 吗?

Can anyone be more descriptive?

谁能描述得更详细一点?

Update 2

更新 2

I am also looking for an answer about how this applies to the thread pool, and what I need to watch out for because of this.

我也在寻找关于这如何适用于线程池的答案,以及因此我需要注意什么。

采纳答案by Stu Mackellar

STA (single-threaded apartment) and MTA (multi-threaded apartment) are to do with COM. COM components can be designed to be accessed by a single thread, in which case it they are hosted in an STA, or they can be made internally thread safe, and hosted in an MTA. A process can have only one MTA, but many STAs. If you're only going to consume COM components all that you really need to know is that you have to match the apartment to the component or nasty things will happen.

STA(单线程单元)和 MTA(多线程单元)与COM 相关。COM 组件可以设计为由单个线程访问,在这种情况下,它们托管在STA 中,或者它们可以在内部线程安全,并托管在MTA 中。一个进程只能有一个 MTA,但可以有许多 STA。如果您只打算使用 COM 组件,那么您真正需要知道的是必须将单元与组件相匹配,否则会发生令人讨厌的事情。

回答by Robert C. Barth

You don't have to worry about it unless you're doing COM-interop, in which case there are marshalling issues. It has no ramifications for .net itself.

除非您正在执行 COM 互操作,否则您不必担心它,在这种情况下会出现编组问题。它对 .net 本身没有影响。

回答by Lou Franco

If your COM object needs to believe that it is in a single-threaded environment, use STA. You are guaranteed that the creation and all calls will be made by the same thread. You can safely use Thread local storage and you don't need to use critical sections.

如果您的 COM 对象需要相信它处于单线程环境中,请使用 STA。您可以保证创建和所有调用将由同一个线程进行。您可以安全地使用 Thread 本地存储,并且不需要使用临界区。

If your COM object can be accessed by many threads simultaneously, use MTA -- there will be no guards put in place.

如果您的 COM 对象可以被多个线程同时访问,请使用 MTA —— 不会有任何保护措施。

回答by Brian Rasmussen

As others have pointed out, it generally has little impact on .NET applications.

正如其他人指出的那样,它通常对 .NET 应用程序几乎没有影响。

However, be aware that the Microsoft test host used for unit tests is actually implemented in an STA, which means that there are limitations on what you can do in unit test. For example you cannot do a WaitAllon a WaitHandlein a unit test is you're using Microsoft's test host.

但是请注意,用于单元测试的 Microsoft 测试主机实际上是在 STA 中实现的,这意味着您可以在单元测试中执行的操作存在限制。例如,你不能做WaitAll一个WaitHandle在单元测试中,你正在使用微软的测试主机。

回答by Brian Rasmussen

In actuality, STAs and MTAs have an impact on .NET code. See Chris Brumme's blog entry for way more detail then you probably need:

实际上,STA 和 MTA 对 .NET 代码有影响。有关更多详细信息,请参阅 Chris Brumme 的博客条目,然后您可能需要:

https://devblogs.microsoft.com/cbrumme/apartments-and-pumping-in-the-clr/

https://devblogs.microsoft.com/cbrumme/apartments-and-pumping-in-the-clr/

It's really important to understand how STAs pump messages in .NET. It does have consequences.

了解 STA 如何在 .NET 中抽取消息非常重要。它确实有后果。