STAThread和多线程 [英] STAThread and multithreading

查看:300
本文介绍了STAThread和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从上STAThread MSDN文章:

From the MSDN article on STAThread:

表示COM线程模型的应用程序是单线程单元(STA)。

Indicates that the COM threading model for an application is single-threaded apartment (STA).

(供参考,这是整篇文章。)

单线程公寓...... OK,那去了我的头。另外,我读的地方,除非你的应用程序使用COM互操作的,这个属性实际上做什么都没有。那么究竟是什么做的,它是如何影响多线程应用程序?应该多线程应用程序(其中包括使用任何人任何事定时器 s到异步方法调用,而不仅仅是线程池等)的使用MTAThread,即使是以防万一?什么是STAThread和MTAThread实际上做?

Single-threaded apartment... OK, that went over my head. Also, I read somewhere that unless your application uses COM interop, this attribute actually does nothing at all. So what exactly does it do, and how does it affect multithreaded applications? Should multithreaded applications (which includes anything from anyone using Timers to asynchronous method calls, not just threadpools and the like) use MTAThread, even if it's 'just to be safe'? What does STAThread and MTAThread actually do?

推荐答案

公寓线程是一个COM概念;如果你不使用COM,并没有调用使用COM在幕后这些API,那么你就不需要担心的公寓。

Apartment threading is a COM concept; if you're not using COM, and none of the APIs you call use COM "under the covers", then you don't need to worry about apartments.

如果你需要知道的公寓,详细信息可以得到一个有点复杂;一个可能是,过于简单​​的说法是,标记为STA COM对象必须在一个STAThread运行,和COM对象标记MTA必须在MTA的线程中运行。使用这些规则,COM可以优化这些不同的对象之间的呼叫,避免了编组其中没有必要

If you do need to be aware of apartments, then the details can get a little complicated; a probably-oversimplified version is that COM objects tagged as STA must be run on an STAThread, and COM objects marked MTA must be run on an MTA thread. Using these rules, COM can optimize calls between these different objects, avoiding marshaling where it isn't necessary.

这篇关于STAThread和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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