By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

What is [MTAThread] & why doesn't VB have it?

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a

"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.


Nov 17 '05 #2

P: n/a

"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
Nov 17 '05 #3

P: n/a
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
Nov 17 '05 #4

P: n/a
Brett <no@spam.net> wrote:
This doesn't answer my question of why I never see that attribute in VB.NET.
Yes it did:

<quote>
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.
</quote>
Also, if I'm only using .NET libraries and not COM/ActiveX, why is the
STA/MTA Thread attribute needed?


If you're using Windows Forms, you basically *are* using COM...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

P: n/a
No they are not - I think there amy be a couple of windows forms controls that are actua\lly wrappers for activeX controls - however the reason you see it is just in case you call a COM component. The effect of the attribute if no COM interop occurs is nothing. The COM interop layer uses its existence to initialize the thread correctly - nothing else

Regards

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

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

John

Nov 17 '05 #6

P: n/a
Brett,
In addition to the other comments

| Also, why doesn't VB use STA or MTA thread?
As Willy suggest VB.NET puts the STAThread attribute into the IL generated,
you will not see it looking at the VB source itself.

If you want to see the attribute in VB.NET you need to use ILDASM.EXE or
Reflector on the assembly that VB.NET creates to look at the IL created. Be
certain to turn on the custom attributes in Reflector to see them...

Hope this helps
Jay
"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
|
|
Nov 17 '05 #7

P: n/a

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

"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.


Sure I did,
.... the compiler generates the code that initializes the thread for STA
(Single Threaded Apartments) by default.

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

Please read again, I mentioned 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.


Some Windows Forms Controls are simple wrappers of AX controls.
But there is more, some of the .NET library classes are wrapping COM
objects, System.Management, System.DirectoryServices,
System.EnterpriseServices just to name a few.

In most cases you don't need to care about this, just mark your main entry
with STAThread and you are covered for most of the use cases, initialize you
own threads to enter the MTA by default, all of the COM objects used
implicitly by the FCL are marked "both" so they prefer an MTA to live in.
The same goes for Services, init your threads for MTA, don't use STA COM
objects in Windows Services, they require a windows message queue and you
have to dispatch the queue (pump messages) so they're not designed for such
environment.

Willy.
Nov 17 '05 #8

P: n/a
>>
This doesn't answer my question of why I never see that attribute in
VB.NET.


Sure I did,
... the compiler generates the code that initializes the thread for STA
(Single
Threaded Apartments) by default.


Maybe I should rephrase the question. Why does one set STA in the compiler
and the other in the code? One don't they both do the same since the
outcome is the same.

Thanks,
Brett
Nov 17 '05 #9

P: n/a
Because C# and VB.NET are not the same language, do not have compilers written by the same people and do not have the same team working on the IDE.

I guess each were looking for the most obvious migration path for developers.VB.NET was looking at VB6 which was mainly focussed around UI work. In this case having the STAThread attribute staring at people when it could be assumed was seen as a distraction so they decided to have the compiler emit it. Whereas C# doesn't have so obvious a migration path and so they left it as more explicit (as most things are in C#)

Regards

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

Maybe I should rephrase the question. Why does one set STA in the compiler
and the other in the code? One don't they both do the same since the
outcome is the same.

Thanks,
Brett

Nov 17 '05 #10

P: n/a
> Maybe I should rephrase the question. Why does one set STA in the
compiler and the other in the code? One don't they both do the same since
the outcome is the same.


Philosophical differences between the two I'd wager. C# tends towards
explicity whereas VB tends towards simplicity.
Nov 17 '05 #11

P: n/a

"Brett" <no@spam.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

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


Sure I did,
... the compiler generates the code that initializes the thread for STA
(Single
Threaded Apartments) by default.


Maybe I should rephrase the question. Why does one set STA in the
compiler and the other in the code? One don't they both do the same since
the outcome is the same.

Thanks,
Brett


I second Richards opinion, it's just a matter of keeping a level of
compatibility with VB6 that did exactly that - initialize the main thread
(the only one) to enter an STA. The C# team didn't have to look back and
they decided (IMO the right thing) to give the developer the opportunity to
select the best option for his COM threading needs.

Willy.
Nov 17 '05 #12

P: n/a
Brett,
I concur with Daniel, Richard & Willy on the "why".

I would also like to add that VB.NET does not require a Main routine at all.
You can select the form itself as the startup object & VB.NET will add a
Main for you to that class.

VB.NET is smart enough that if you add a Main & add a STAThread or a
MTAThread attribute then it will not add the STAThread attribute for you.

Hope this helps
Jay

"Brett" <no@spam.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
| >>
| >> This doesn't answer my question of why I never see that attribute in
| >> VB.NET.
| >>
| >
| > Sure I did,
| > ... the compiler generates the code that initializes the thread for STA
| > (Single
| >> Threaded Apartments) by default.
|
| Maybe I should rephrase the question. Why does one set STA in the
compiler
| and the other in the code? One don't they both do the same since the
| outcome is the same.
|
| Thanks,
| Brett
|
|
Nov 17 '05 #13

P: n/a
Jon Skeet [C# MVP] wrote:
Brett <no@spam.net> wrote:
Also, if I'm only using .NET libraries and not COM/ActiveX, why is the
STA/MTA Thread attribute needed?


If you're using Windows Forms, you basically *are* using COM...


If you look into the windows forms code using reflector you see they
send a lot of Win32 messages, i.e.: they use win32 glyphs, not COM
controls, at least the code I peeked into. There might be COM used
elsewhere (as said by Willy) but I don't see the COM link (no pun
intended ;)) with the winforms code as it's plain win32 (for what I've
seen, so it might be I peeked in the wrong closet ;))

FB
Nov 17 '05 #14

P: n/a

"Frans Bouma [C# MVP]" <pe******************@xs4all.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Jon Skeet [C# MVP] wrote:
Brett <no@spam.net> wrote:
Also, if I'm only using .NET libraries and not COM/ActiveX, why is the
STA/MTA Thread attribute needed?


If you're using Windows Forms, you basically *are* using COM...


If you look into the windows forms code using reflector you see they send
a lot of Win32 messages, i.e.: they use win32 glyphs, not COM controls, at
least the code I peeked into. There might be COM used elsewhere (as said
by Willy) but I don't see the COM link (no pun intended ;)) with the
winforms code as it's plain win32 (for what I've seen, so it might be I
peeked in the wrong closet ;))

FB


Frans,
Most of the controls like Button, ListBox etc.. are just native Win32
controls (Comctl32.dll and ComDlg.dll) other more complex controls may need
an STA to run correctly, because they call into COM, one example is the
FolderBrowser [1] and RichTextBox control (itself an AX control site).
V2.0 adds a few new controls that are managed wrappers around existing AX
controls, like the IE browser control and Active Document Control container.
And there is always the good old clipboard and Ole Drag and Drop which
requires an STA to run in.

[1]
http://pluralsight.com/blogs/craig/a...2/22/1219.aspx

Willy.

Nov 17 '05 #15

P: n/a
Willy Denoyette [MVP] wrote:
"Frans Bouma [C# MVP]" <pe******************@xs4all.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Jon Skeet [C# MVP] wrote:
Brett <no@spam.net> wrote:

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

If you're using Windows Forms, you basically *are* using COM...


If you look into the windows forms code using reflector you see they send
a lot of Win32 messages, i.e.: they use win32 glyphs, not COM controls, at
least the code I peeked into. There might be COM used elsewhere (as said
by Willy) but I don't see the COM link (no pun intended ;)) with the
winforms code as it's plain win32 (for what I've seen, so it might be I
peeked in the wrong closet ;))


Frans,
Most of the controls like Button, ListBox etc.. are just native Win32
controls (Comctl32.dll and ComDlg.dll) other more complex controls may need
an STA to run correctly, because they call into COM, one example is the
FolderBrowser [1] and RichTextBox control (itself an AX control site).
V2.0 adds a few new controls that are managed wrappers around existing AX
controls, like the IE browser control and Active Document Control container.
And there is always the good old clipboard and Ole Drag and Drop which
requires an STA to run in.


ah, thanks for the info, Willy! :)

Frans

Nov 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.