与MTA线程相比,STA线程有哪些局限性? [英] What are the limitations of a STA thread in compare to MTA threads?

查看:141
本文介绍了与MTA线程相比,STA线程有哪些局限性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们这样创建线程STA:Thread.SetApartmentState(STA);,则它无法运行标有[MTAThread]属性的代码.

If we make a thread STA like this: Thread.SetApartmentState(STA); then it cannot run code marked with [MTAThread] attribute.

我们已经在Windows和控制台应用程序中看到[STAThread],但是我从未见过具有[MTAThread]属性的代码,并且不知道哪个.NET库使用此属性.

We have seen [STAThread] in windows and console applications but I have never seen code with [MTAThread] attribute and don't know which .NET libraries use this attribute.

我的问题是,与具有MTA公寓状态(自然的.NET线程)的线程相比,将公寓状态设置为STA的线程的局限性是什么?

My question is what are the limitations of a thread with apartment state set to STA, in compare to threads with MTA apartment state (natural .NET threads) ?

推荐答案

那么它就不能运行标有[MTAThread]属性的代码.

then it cannot run code marked with [MTAThread] attribute.

这不是它的工作方式.公寓类型是线程的属性,而不是方法的属性.您会看到[STAThread]属性仅应用于.NET程序的Main()方法.它确定为运行程序而创建的第一个线程的单元类型.必要的,因为您无法在线程运行后调用SetApartmentState().除此之外,该属性没有意义,线程在其生命周期内停留在STA中.您永远不会看到[MTAThread],因为这是默认设置.

That's not how it works. The apartment type is a property of a thread, not of a method. You see the [STAThread] attribute applied only to the Main() method of a .NET program. It determines the apartment type of the very first thread that is created to run the program. Necessary because you can't call SetApartmentState() after the thread is running. Beyond that, the attribute has no meaning, the thread stays in an STA for its lifetime. You never see [MTAThread] because that's the default.

STA线程有一些限制.它永远不会阻塞,因为这将阻塞并经常使试图调用单元线程COM对象方法的任何代码死锁.并且它必须泵送消息循环,以便COM可以封送来自另一个线程的方法调用.封送方法调用只能在线程为空闲"时执行,而不能忙于执行任何代码.消息循环提供了不忙"状态.

A thread that's STA has some limitations. It can never block because that will block and often deadlock any code that tries to call a method of an apartment threaded COM object. And it must pump a message loop so that COM can marshal the method call from another thread. Marshaled method calls can only be executed when a thread is 'idle', not busy executing any code. A message loop provides that 'not busy' state.

对COM组件也有要求.它必须支持封送处理,方法是将自身限制为自动化支持的类型的子集,以便可以使用标准封送处理器.或通过提供代理/存根对进行自定义封送处理. HKCR\Interface\{iid}\ProxyStubClsid32注册表项确定封送处理的方式.

There are requirements on the COM component as well. It must support marshaling, either by restricting itself to the subset of types that are supported by Automation so that the standard marshaller can be used. Or by providing a proxy/stub pair for custom marshaling. The HKCR\Interface\{iid}\ProxyStubClsid32 registry key determines how the marshaling is done.

显式支持在STA和MTA线程之间共享单元线程对象. STA线程必须创建它,对MTA线程(或其他STA线程)上的所有调用进行封送处理.这确保了该组件仅能看到在同一线程上进行的调用,从而确保了线程安全性.不需要其他锁定.

Sharing an apartment threaded object between an STA and an MTA thread is explicitly supported. The STA thread must create it, any calls on the MTA thread (or other STA threads) is marshaled. Which ensures that the component only ever sees calls made on the same thread, thus ensuring thread-safety. No additional locking is required.

最后但并非最不重要的一点是,如果您在MTA线程上创建单元线程的COM对象,则COM将自动创建STA线程以为其提供安全的家.唯一的失败模式是COM组件不支持封送处理.这样做的一个缺点是每次调用都会被封送.太慢了.

Last but not least, if you create an apartment threaded COM object on an MTA thread then COM will automatically create an STA thread to give it a safe home. The only failure mode for this is when the COM component doesn't support marshaling. The one disadvantage of doing it this way is that every call will be marshaled. That's slow.

这篇关于与MTA线程相比,STA线程有哪些局限性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆