什么是[MTAThread]& VB为什么没有它? [英] What is [MTAThread] & why doesn't VB have it?

查看:119
本文介绍了什么是[MTAThread]& VB为什么没有它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#代码自动生成[STAThread]或[MTAThread]引用。

单个或多个公寓线程。如果我删除了它会做什么以及将会发生什么?


如果我想在我的应用程序中使用多个线程,是否需要MTAThread?


另外,为什么VB不使用STA或MTA线程?


谢谢,

Brett

解决方案



" Brett" < no@spam.net>在消息中写道

新闻:OT ************** @ TK2MSFTNGP09.phx.gbl ...

C#代码自动生成[ STAThread]或[MTAThread]引用。
单个或多个公寓线程。如果我删除它会做什么以及将会发生什么?

如果我想在我的应用程序中使用多个线程,MTAThread是否必要?

此外,为什么VB不使用STA或MTA线程?

谢谢,
Brett




VS for VB.NET没有不要在主条目上设置此属性,但编译器

默认生成初始化STA线程的代码(Single Threaded

Apartments)。

C#将此属性设置为[STAThread],因为它在Windows窗体中需要

应用程序,其他类型的应用程序需要明确设置它,具体取决于

on COM要求。


现在为什么需要在C#中设置属性?

它告诉编译器发出代码来初始化COM的线程

互操作。 Windows Forms需要它有两个原因:

1.剪切和粘贴支持,这是一种需要STA的OLE技术。

2 COM与ActiveX控件互操作(直接或间接地)用作UI

元素。

AX COM对象是他们需要在STA线程上运行的所有STA类型COM对象

和他们只能直接从同一个线程调用,来自其他

线程的调用会被自动封送,这意味着开销和序列化

(一次只能调用一次)。 />

MTA(多线程公寓)类型的COM对象(也称为免费

线程)需要一个MTA初始化线程才能运行但可以从任何
线程(同时)不会产生编组开销。


所以,你肯定需要Windows应用程序的STAThread属性。

大多数控制台应用程序可以安全地使用STAThread,但如果你需要,你最好设置

MTAThread属性d调用自由线程来自主线程的COM对象

。 COM对象标记为线程模型两者。可以在STA和MTA上运行
,但最好在STA线程上运行它们。

Windows服务不承载ActiveX对象(它们不应该)所以这里有

不需要(你也不能)设置这个属性。如果你需要从服务中调用

COM,你必须通过在代码中设置线程的

AppartmentState属性来初始化线程。


Willy。





" Willy Denoyette [MVP]" <无线************* @ telenet.be>在消息中写道

新闻:嗯************** @ TK2MSFTNGP10.phx.gbl ...


" ;布雷特" < no@spam.net>在消息中写道
新闻:OT ************** @ TK2MSFTNGP09.phx.gbl ...

C#代码自动生成[STAThread]或[ MTAThread]参考。
单人或多人公寓线程。如果我删除它会做什么以及将会发生什么?

如果我想在我的应用中使用多个线程,MTAThread是否必要?

此外,为什么VB不使用STA或MTA线程?

谢谢,
Brett



VS for VB.NET没有设置这个主条目上的属性,但
编译器生成默认情况下为STA(Single
Threaded Apartments)初始化线程的代码。
C#将此属性设置为[STAThread],因为它''在Windows中需要
表单应用程序,其他类型的应用程序需要根据COM要求明确设置它。

现在为什么需要在C#中设置属性?
它告诉编译器发出代码来初始化COM
interop的线程。 Windows Forms需要它有两个原因:
1.剪切和粘贴支持,这是一种需要STA的OLE技术。
2 COM与ActiveX控件(直接或间接)互操作用作UI
元素。
AX COM对象是他们需要在STA
线程上运行的所有STA类型的COM对象,它们只能直接从同一个线程调用,从其他线程自动调用
marshaled,这意味着开销和序列化(一次只能调用一次)。

MTA(多线程公寓)类型的COM对象(也称为免费线程)需要一个MTA初始化线程运行,但可以从任何线程(同时)调用,而不会产生编组开销。

因此,您肯定需要Windows应用程序的STAThread属性。
大多数控制台应用程序都可以安全地使用STAThread,但如果需要调用free threaded,则最好设置
MTAThread属性。来自主线程的COM对象。 COM对象标记为线程模型两者。可以在STA和MTA上运行
,但最好在STA线程上运行它们。
Windows服务不能托管ActiveX对象(它们不应该)所以在这里
是没有必要(你不能)设置这个属性。如果你需要从服务调用
COM,你必须通过在代码中设置线程的
AppartmentState属性来初始化线程。

Willy。




这不能回答我为什么在VB.NET中看不到这个属性的问题。


另外,如果我'' m只使用.NET库而不是COM / ActiveX,为什么需要

STA / MTA Thread属性?


Brett


Brett写道:

" Willy Denoyette [MVP]" <无线************* @ telenet.be>在消息中写道
新闻:嗯************** @ TK2MSFTNGP10.phx.gbl ...

" Brett" < no@spam.net>在消息中写道
新闻:OT ************** @ TK2MSFTNGP09.phx.gbl ...

C#代码自动生成[ STAThread]或[MTAThread]引用。
单个或多个公寓线程。如果我删除它会做什么以及将会发生什么?

如果我想在我的应用中使用多个线程,MTAThread是否必要?

此外,为什么VB不使用STA或MTA线程?

谢谢,
Brett



VS for VB.NET没有设置这个主条目上的属性,但
编译器生成默认情况下为STA(Single
Threaded Apartments)初始化线程的代码。
C#将此属性设置为[STAThread],因为它''在Windows中需要
表单应用程序,其他类型的应用程序需要根据COM要求明确设置它。

现在为什么需要在C#中设置属性?
它告诉编译器发出代码来初始化COM
interop的线程。 Windows Forms需要它有两个原因:
1。剪切和粘贴支持,这是一种需要STA的OLE技术。
2 COM与ActiveX控件(直接或间接)互操作用作UI
元素。
AX COM对象都是STA类型他们需要在STA
线程上运行的COM对象,它们只能直接从同一个线程调用,来自其他线程的调用自动封送,这意味着开销和序列化(只有一个)一次调用)。

MTA(多线程公寓)类型的COM对象(也称为免费线程)需要一个MTA初始化线程才能运行但可以从
任何线程(同时)不会产生编组开销。

所以,你肯定需要Windows应用程序的STAThread属性。
大多数控制台应用程序可以安全地使用STAThread,但你最好设置
MTAThread属性,如果你需要调用自由线程来自主线程的COM对象。 COM对象标记为线程模型两者。可以在STA和MTA上运行
,但最好在STA线程上运行它们。
Windows服务不能托管ActiveX对象(它们不应该)所以在这里
是没有必要(你不能)设置这个属性。如果你需要从服务调用
COM,你必须通过在代码中设置线程的
AppartmentState属性来初始化线程。

Willy。



这不能回答我为什么在VB.NET中看不到这个属性的问题。

另外,如果我只使用.NET库而不是COM / ActiveX,为什么需要
STA / MTA Thread属性?

Brett



我相信很多.Net图书馆也写在COM中。


John


C# code automatically generates [STAThread] or [MTAThread] references.
Single or Multi apartment threads. What exacty does this do and what will
happen if I remove it?

Is MTAThread necessary if I want to use multiple threads in my app?

Also, why doesn''t VB use STA or MTA thread?

Thanks,
Brett

解决方案


"Brett" <no@spam.net> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...

C# code automatically generates [STAThread] or [MTAThread] references.
Single or Multi apartment threads. What exacty does this do and what will
happen if I remove it?

Is MTAThread necessary if I want to use multiple threads in my app?

Also, why doesn''t VB use STA or MTA thread?

Thanks,
Brett



VS for VB.NET doesn''t set this attribute on the main entry, but the compiler
generates the code that initializes the thread for STA (Single Threaded
Apartments) by default.
C# sets this attribute to [STAThread] because it''s required in Windows Forms
applications, other type of applications need to set it explicitly depending
on the COM requirements.

Now why is it required to set the attribute in C#?
It tells the compiler to emit the code to initialize the thread for COM
interop. Windows Forms needs it for two reasons:
1. Cut and paste support, which is an OLE technology that requires a STA.
2 COM interop with ActiveX controls (directly or indirectly) used as UI
elements.
AX COM objects are all STA type COM objects they need to run on a STA thread
and they can only be called directly from this same thread, calls from other
threads are automatically marshaled, which implies overhead and serialized
(only one call at a time).

MTA (Multi Threaded Apartment) type of COM objects (also called free
threaded) need a MTA initialized thread to run on but can be called from any
thread (simultaneously) without incurring a marshaling overhead.

So, you definitely need the STAThread attribute for Windows applications.
Most console applications can safely use STAThread, but you better set the
MTAThread attribute if you need to call into "free threaded" COM objects
from your main thread. COM object marked as threading model "both" can run
on both STA and MTA, but its better to run them on STA threads.
Windows Services don''t host ActiveX objects (they shouldn''t) so here there
is no need (and you can''t) to set this attribute. If you need to call into
COM from a Service you have to initialize the thread by setting the
AppartmentState property of the thread in your code.

Willy.




"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...


"Brett" <no@spam.net> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...

C# code automatically generates [STAThread] or [MTAThread] references.
Single or Multi apartment threads. What exacty does this do and what
will happen if I remove it?

Is MTAThread necessary if I want to use multiple threads in my app?

Also, why doesn''t VB use STA or MTA thread?

Thanks,
Brett



VS for VB.NET doesn''t set this attribute on the main entry, but the
compiler generates the code that initializes the thread for STA (Single
Threaded Apartments) by default.
C# sets this attribute to [STAThread] because it''s required in Windows
Forms applications, other type of applications need to set it explicitly
depending on the COM requirements.

Now why is it required to set the attribute in C#?
It tells the compiler to emit the code to initialize the thread for COM
interop. Windows Forms needs it for two reasons:
1. Cut and paste support, which is an OLE technology that requires a STA.
2 COM interop with ActiveX controls (directly or indirectly) used as UI
elements.
AX COM objects are all STA type COM objects they need to run on a STA
thread and they can only be called directly from this same thread, calls
from other threads are automatically marshaled, which implies overhead and
serialized (only one call at a time).

MTA (Multi Threaded Apartment) type of COM objects (also called free
threaded) need a MTA initialized thread to run on but can be called from
any thread (simultaneously) without incurring a marshaling overhead.

So, you definitely need the STAThread attribute for Windows applications.
Most console applications can safely use STAThread, but you better set the
MTAThread attribute if you need to call into "free threaded" COM objects
from your main thread. COM object marked as threading model "both" can run
on both STA and MTA, but its better to run them on STA threads.
Windows Services don''t host ActiveX objects (they shouldn''t) so here there
is no need (and you can''t) to set this attribute. If you need to call into
COM from a Service you have to initialize the thread by setting the
AppartmentState property of the thread in your code.

Willy.



This doesn''t answer my question of why I never see that attribute in VB.NET.

Also, if I''m only using .NET libraries and not COM/ActiveX, why is the
STA/MTA Thread attribute needed?

Brett


Brett wrote:

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...

"Brett" <no@spam.net> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...

C# code automatically generates [STAThread] or [MTAThread] references.
Single or Multi apartment threads. What exacty does this do and what
will happen if I remove it?

Is MTAThread necessary if I want to use multiple threads in my app?

Also, why doesn''t VB use STA or MTA thread?

Thanks,
Brett



VS for VB.NET doesn''t set this attribute on the main entry, but the
compiler generates the code that initializes the thread for STA (Single
Threaded Apartments) by default.
C# sets this attribute to [STAThread] because it''s required in Windows
Forms applications, other type of applications need to set it explicitly
depending on the COM requirements.

Now why is it required to set the attribute in C#?
It tells the compiler to emit the code to initialize the thread for COM
interop. Windows Forms needs it for two reasons:
1. Cut and paste support, which is an OLE technology that requires a STA.
2 COM interop with ActiveX controls (directly or indirectly) used as UI
elements.
AX COM objects are all STA type COM objects they need to run on a STA
thread and they can only be called directly from this same thread, calls
from other threads are automatically marshaled, which implies overhead and
serialized (only one call at a time).

MTA (Multi Threaded Apartment) type of COM objects (also called free
threaded) need a MTA initialized thread to run on but can be called from
any thread (simultaneously) without incurring a marshaling overhead.

So, you definitely need the STAThread attribute for Windows applications.
Most console applications can safely use STAThread, but you better set the
MTAThread attribute if you need to call into "free threaded" COM objects
from your main thread. COM object marked as threading model "both" can run
on both STA and MTA, but its better to run them on STA threads.
Windows Services don''t host ActiveX objects (they shouldn''t) so here there
is no need (and you can''t) to set this attribute. If you need to call into
COM from a Service you have to initialize the thread by setting the
AppartmentState property of the thread in your code.

Willy.


This doesn''t answer my question of why I never see that attribute in VB.NET.

Also, if I''m only using .NET libraries and not COM/ActiveX, why is the
STA/MTA Thread attribute needed?

Brett


I believe a lot of .Net libraries are also written in COM.

John


这篇关于什么是[MTAThread]&amp; VB为什么没有它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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