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

B

Brett

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
 
W

Willy Denoyette [MVP]

Brett said:
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.
 
B

Brett

Willy Denoyette said:
Brett said:
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
 
J

Jianwei Sun

Brett said:
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
 
J

Jon Skeet [C# MVP]

Brett said:
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.
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...
 
R

Richard Blewett [DevelopMentor]

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
 
J

Jay B. Harlow [MVP - Outlook]

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


| 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
|
|
 
W

Willy Denoyette [MVP]

Brett said:
Willy Denoyette said:
Brett said:
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:

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

Brett

Sure I did,
... the compiler generates the code that initializes the thread for STA
(Single

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
 
D

Daniel O'Connell [C# MVP]

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

Richard Blewett [DevelopMentor]

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
 
W

Willy Denoyette [MVP]

Brett said:
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.
 
J

Jay B. Harlow [MVP - Outlook]

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

| >>
| >> 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
|
|
 
F

Frans Bouma [C# MVP]

Jon said:
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
 
W

Willy Denoyette [MVP]

Frans Bouma said:
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/archive/2004/02/22/1219.aspx

Willy.
 
F

Frans Bouma [C# MVP]

Willy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top