STA和MTA之间的区别 [英] difference between STA and MTA

查看:93
本文介绍了STA和MTA之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我读了数千万篇关于STA和MTA的文章:-)。所有的东西

非常理论化,而且我没有得到它...

任何人都可以解释我[STAThread]和[MTAThread]在一个漂亮的,小的

和简单的例子,我真的可以看到这两种方法之间的区别......


他们都在谈论2消息循环和com互操作......但是当使用STA或MTA时,我会得到什么?b $ b $

thanx,jazper

hi

i read thousands of millions of articles about STA and MTA :-). everything
very theoretical and i don''t get it...
could anybody explain me the [STAThread] and [MTAThread] on a nice, small
and simple example where i really can see the difference between these two
methods...

they''re all talking about 2 message loops and com interop... but what
benefit does i get when using either STA or MTA?

thanx, jazper

推荐答案

哇数千万篇文章很多;-)


文章在谈到STAThread时引用COM互操作的原因和MTAThread是因为这些属性在执行COM互操作时只有任何相关性。当没有COM互操作时,它们没有任何影响。


那么您是否了解COM中的STA和MTA?


问候

Richard Blewett - DevelopMentor
http:/ /www.dotnetconsult.co.uk/weblog
http:// www.dotnetconsult.co.uk


hi


i阅读有关STA和MTA的数千万篇文章:-)。所有的东西

非常理论化,而且我没有得到它...

任何人都可以解释我[STAThread]和[MTAThread]在一个漂亮的,小的

和简单的例子,我真的可以看到这两种方法之间的区别......


他们都在谈论2消息循环和com互操作......但是当使用STA或MTA时,我得到了什么?b $ b $

thanx,jazper
Wow thousands of millions is alot of articles ;-)

The reason articles refer to COM interop when they talk about STAThread and MTAThread is because those attributes only have any relevance when you are performing COM interop. They have no impact whatsoever when no COM interop occurs.

So do you understand STAs and MTA in COM terms?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

hi

i read thousands of millions of articles about STA and MTA :-). everything
very theoretical and i don''t get it...
could anybody explain me the [STAThread] and [MTAThread] on a nice, small
and simple example where i really can see the difference between these two
methods...

they''re all talking about 2 message loops and com interop... but what
benefit does i get when using either STA or MTA?

thanx, jazper


正如理查德所说,STA'和MTA'是COM的文物。


在16位Windows的早期,只有所有操作系统中都有一个线程,所以没有人开发COM对象然后需要担心他们的代码是从多个线程调用的。没有人

需要同步对象,例如事件和互斥体。美好时光

四周:)


当Win32出现时,你突然发现自己拥有所有这些线程

可能会调用进入你唯一的代码只是

设计为从一个且只有一个线程调用。该怎么办?

是为16位Windows开发的很多COM对象,在数以千万计的应用程序中使用了
(;))并不可行

只是扔掉它们因为我们现在终于可以做多任务了。


解决方案就是公寓。 16位Windows中的COM应用程序已经在开始使用COM之前必须执行一些初始化,他们不得不调用方法CoInitialize()来调用
。所以这是一种识别

COM应用程序的方法,这些应用程序对线程一无所知。如果你打电话给

CoInitialize你不知道线程是什么,如果你在一个线程上创建了一个

对象,你就假设对该对象的所有调用都进来了在

相同的线程。很酷!


你需要在使用任何

之前初始化COM的要求被保留了,但现在你必须在每一个上执行初始化

即将使用或创建COM对象的线程。设计时考虑到线程的那些应用程序

可以通过调用

新方法,CoInitializeEx来说明,如果他们允许来电话说明
$ b来自任何线程或来自一个线程的$ b。


任何调用CoInitialize()的线程都被称为进入Single

线程公寓(STA) 。在那个公寓里只能住一个

的帖子。如果有人从另一个线程调用一个在STA中的线程上创建的对象,那么COM运行时会被称为将一个线程中的代码从另一个线程封送到另一个线程。 。这是通过将窗口

消息发布到STA线程来实现的。 Windows消息将被放入线程

消息队列中,这使得线程有机会一个接一个地处理它们并且很容易处理它们。这就是为什么STA中的线程必须包含消息泵的原因。

线程可以调用CoInitializeEx并声明此处的代码可以处理来自多个的
调用线程,它具有所有的互斥体和关键的部分或其他任何东西。据说这些线程进入Multi

螺纹公寓。由MTA中的线程创建的对象可以由MTA中任何其他线程上的代码直接调用。没有

编组和windows消息巫毒必备。


Pheew!


所以当.NET应用程序是关于通过互操作调用COM对象,它会检查当前线程的单元状态,可以是MTA,

。如果设置了MTA或Unknown,则使用多线程参数调用
CoInitializeEx()。这个

使COM运行时能够在需要时使用编组并跳过它

不需要它。


线程可以使用任何线程的

Threading.ApartmentState属性或通过将

STAThread或MTAThread应用于应用程序的Main方法来设置公寓状态。


Jeeesh,多长的帖子!还有更多的细节,但是我想b $ b认为这涵盖了基础知识。


问候,

Joakim

Richard Blewett [DevelopMentor]写道:
As Richard said, STA''s and MTA''s are artifacts of COM.

In the early days of 16 bit Windows there was only one thread in all of
the operating system, so no one developing COM-objects then needed to
worry about if their code was called from multiple threads. No one
needed synchronization objects such as events and mutexes. Good times
all around :)

When Win32 came along you suddenly found yourself with all these threads
that could potentially call into your nice piece of code that was only
designed to be called from one and only one thread. What to do? There
had been a lot of COM-objects developed for 16-bit Windows that was used
in thousands of millions of applications (;)) and it was not feasible to
just throw them away because we now finally could do multitasking.

The solution was apartments. COM applications in 16-bit Windows already
had to perform some initialization prior to begin using COM, they had to
call the method CoInitialize(). So here was a way to identify
COM-applications that don''t know anything about threading. If you call
CoInitialize you don''t know what threads are and if you have created an
object on a thread, you assume that all calls to that object come in on
the same thread. Cool!

The requirement that you need to initialize COM before you could use any
of it was kept, but now you had to perform that initialization on every
thread that was about to use or create a COM object. Those applications
that was designed with threading in mind could state that by calling a
new method, CoInitializeEx, and state if they allowed calls coming in
from any thread or from just the one thread.

Any thread that calls CoInitialize() is said to enter the Single
Threaded Apartment (STA). In that apartment there can live only one
thread. If someone calls an object that is created on a thread in an STA
from another thread, the COM-runtime is said to marshal the code from
one thread to the other. This is accomplished by posting windows
messages to the STA thread. Windows messages will be put in the threads
message queue, which gives the thread a chance to handle them nice and
easy one by one. This is why a thread in an STA must contain a message pump.

A thread can call CoInitializeEx and state that the code here can handle
calls from multiple threads, that it has all the mutexes and critical
sections or whatever in check. These threads are said to enter the Multi
Threaded Apartment. An object created by a thread in the MTA can be
called directly by code on any other thread that is also in the MTA. No
marshalling and windows messaging voodoo necessary.

Pheew!

So when a .NET application is about to call a COM-object via interop, it
checks the current threads apartment state, which can either be MTA,
STA, or unknown. If the threads apartment state is STA, CoInitialize()
is called before interop is performed. If MTA or Unknown is set,
CoInitializeEx() is called with the multithreaded parameter. This
enables the COM-runtime to use marshalling where needed and skip it
where it is not needed.

The thread''s apartment state can be set by using the
Threading.ApartmentState property for any thread or by applying the
STAThread or MTAThread to the application''s Main method.

Jeeesh, what a long post! There are a lot more details to it, but I
think this covers the basics.

Regards,
Joakim
Richard Blewett [DevelopMentor] wrote:
哇数千万篇文章很多;-)

文章在谈话时提到COM互操作的原因关于STAThread和MTAThread是因为这些属性在执行COM互操作时只有任何相关性。当没有COM互操作时,它们没有任何影响。

那么您是否了解COM中的STA和MTA?

关于

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk <


我读了数千万篇关于STA和MTA的文章:-)。一切都非常理论化,我不明白......
任何人都可以解释一下[STAThread]和[MTAThread]一个很好的,小的
和简单的例子,我真的可以看看这两种方法之间的区别......

他们都在谈论2个消息循环和com互操作...但是在使用时我会得到什么好处?无论是STA还是MTA?

thanx,jazper
Wow thousands of millions is alot of articles ;-)

The reason articles refer to COM interop when they talk about STAThread and MTAThread is because those attributes only have any relevance when you are performing COM interop. They have no impact whatsoever when no COM interop occurs.

So do you understand STAs and MTA in COM terms?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

hi

i read thousands of millions of articles about STA and MTA :-). everything
very theoretical and i don''t get it...
could anybody explain me the [STAThread] and [MTAThread] on a nice, small
and simple example where i really can see the difference between these two
methods...

they''re all talking about 2 message loops and com interop... but what
benefit does i get when using either STA or MTA?

thanx, jazper



一些错误....

从来没有一个适用于16位窗口的COM版本。所以从来没有基于COM的16位应用程序。


Willy。


" Joakim Karlsson" ; < JK ******* @ NOSPAMjkarlsson.com>在消息中写道

新闻:OF **************** @ TK2MSFTNGP14.phx.gbl ...
A few mistakes....
There''s never been a version of COM for 16 bit windows. So there have never
been 16 bit applications based on COM.

Willy.

"Joakim Karlsson" <jk*******@NOSPAMjkarlsson.com> wrote in message
news:OF****************@TK2MSFTNGP14.phx.gbl...
正如理查德所说,STA和MTA是COM的工件。

在16位Windows的早期,操作系统中只有一个线程,所以没有人然后开发COM对象需要担心他们的代码是否是从多个线程调用的。没有人需要
同步对象,如事件和互斥体。周围的好时光
:)

当Win32出现时,你突然发现自己拥有所有这些线程
可能会调用你唯一的代码只是
旨在从一个且仅一个线程调用。该怎么办?有许多为16位Windows开发的COM对象,用于数千万个应用程序(;))并且将它们扔掉是不可行的。因为我们现在终于可以做多任务了。

解决方案是公寓。 16位Windows中的COM应用程序已经开始使用COM之前必须执行一些初始化,他们必须调用方法CoInitialize()。所以这是一种识别对线程不了解的COM应用程序的方法。如果你打电话给CoInitialize你不知道线程是什么,如果你在一个线程上创建了一个
对象,你就假设所有对该对象的调用都在同一个线程上。很酷!

在你可以使用它之前需要初始化COM的要求被保留了,但是现在你必须在每个
线程上执行初始化使用或创建COM对象。设计时考虑到线程的那些应用程序可以通过调用新的方法,CoInitializeEx来说明,如果它们允许来自任何线程或仅来自一个线程的调用,则说明。

任何调用CoInitialize()的线程都会进入Single Threaded
Apartment(STA)。在那个公寓里只能住一个线程。如果某人从另一个线程调用在STA中的线程上创建的对象,则称COM运行时将代码从一个线程封送到
其他线程。这是通过将Windows消息发布到STA线程来实现的。
Windows消息将被放入线程消息队列中,这使得
线程有机会逐个处理它们。这就是为什么STA中的一个
线程必须包含一个消息泵。

一个线程可以调用CoInitializeEx并声明这里的代码可以处理来自多个线程的
调用,它拥有所有互斥锁和关键部分或其他任何东西。据说这些线程进入Multi
Threaded Apartment。由MTA中的线程创建的对象可以通过MTA中任何其他线程上的代码直接调用。不需要编组和Windows消息传递voodoo。

Pheew!

因此,当.NET应用程序即将通过互操作调用COM对象时,它会/>检查当前线程的公寓状态,可以是MTA,STA,
或未知。如果线程单元状态是STA,则在执行互操作之前调用CoInitialize()。如果设置了MTA或Unknown,则使用多线程参数调用CoInitializeEx()。这使得COM运行时可以在需要的地方使用编组,并在不需要的地方跳过它。

可以使用<设置线程的公寓状态br />任何线程的Threading.ApartmentState属性,或者将
STAThread或MTAThread应用于应用程序的Main方法。

Jeeesh,多长的帖子!还有更多细节,但我认为
这涵盖了基础知识。

问候,
Joakim

Richard Blewett [DevelopMentor]写道:
As Richard said, STA''s and MTA''s are artifacts of COM.

In the early days of 16 bit Windows there was only one thread in all of
the operating system, so no one developing COM-objects then needed to
worry about if their code was called from multiple threads. No one needed
synchronization objects such as events and mutexes. Good times all around
:)

When Win32 came along you suddenly found yourself with all these threads
that could potentially call into your nice piece of code that was only
designed to be called from one and only one thread. What to do? There had
been a lot of COM-objects developed for 16-bit Windows that was used in
thousands of millions of applications (;)) and it was not feasible to just
throw them away because we now finally could do multitasking.

The solution was apartments. COM applications in 16-bit Windows already
had to perform some initialization prior to begin using COM, they had to
call the method CoInitialize(). So here was a way to identify
COM-applications that don''t know anything about threading. If you call
CoInitialize you don''t know what threads are and if you have created an
object on a thread, you assume that all calls to that object come in on
the same thread. Cool!

The requirement that you need to initialize COM before you could use any
of it was kept, but now you had to perform that initialization on every
thread that was about to use or create a COM object. Those applications
that was designed with threading in mind could state that by calling a new
method, CoInitializeEx, and state if they allowed calls coming in from any
thread or from just the one thread.

Any thread that calls CoInitialize() is said to enter the Single Threaded
Apartment (STA). In that apartment there can live only one thread. If
someone calls an object that is created on a thread in an STA from another
thread, the COM-runtime is said to marshal the code from one thread to the
other. This is accomplished by posting windows messages to the STA thread.
Windows messages will be put in the threads message queue, which gives the
thread a chance to handle them nice and easy one by one. This is why a
thread in an STA must contain a message pump.

A thread can call CoInitializeEx and state that the code here can handle
calls from multiple threads, that it has all the mutexes and critical
sections or whatever in check. These threads are said to enter the Multi
Threaded Apartment. An object created by a thread in the MTA can be called
directly by code on any other thread that is also in the MTA. No
marshalling and windows messaging voodoo necessary.

Pheew!

So when a .NET application is about to call a COM-object via interop, it
checks the current threads apartment state, which can either be MTA, STA,
or unknown. If the threads apartment state is STA, CoInitialize() is
called before interop is performed. If MTA or Unknown is set,
CoInitializeEx() is called with the multithreaded parameter. This enables
the COM-runtime to use marshalling where needed and skip it where it is
not needed.

The thread''s apartment state can be set by using the
Threading.ApartmentState property for any thread or by applying the
STAThread or MTAThread to the application''s Main method.

Jeeesh, what a long post! There are a lot more details to it, but I think
this covers the basics.

Regards,
Joakim
Richard Blewett [DevelopMentor] wrote:
哇数千万篇文章很多;-)
文章在谈到STAThread和MTAThread时引用COM互操作的原因是因为这些属性
你正在进行COM互操作时才有任何相关性。当没有发生COM互操作时,它们没有任何影响。

那么您是否了解COM中的STA和MTA?

问候
Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk / weblog
http://www.dotnetconsult.co.uk


我阅读了数千万篇关于STA和MTA的文章:-)。
一切都非常理论化,我不懂...
任何人都可以在一个不错的,小的
和简单的例子中向我解释[STAThread]和[MTAThread],我真的可以看到这两个之间的区别 br />方法......
他们都在谈论2个消息循环和com互操作...但是当使用STA或MTA时我得到的好处是什么?
thanx, jazper
Wow thousands of millions is alot of articles ;-)

The reason articles refer to COM interop when they talk about STAThread
and MTAThread is because those attributes only have any relevance when
you are performing COM interop. They have no impact whatsoever when no
COM interop occurs.

So do you understand STAs and MTA in COM terms?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

hi
i read thousands of millions of articles about STA and MTA :-).
everything
very theoretical and i don''t get it...
could anybody explain me the [STAThread] and [MTAThread] on a nice,
small
and simple example where i really can see the difference between these
two
methods...
they''re all talking about 2 message loops and com interop... but what
benefit does i get when using either STA or MTA?
thanx, jazper



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

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