473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
15 21264

"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

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

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

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: NotGiven | last post by:
session_start(); if (isset($HTTP_SESSION_VARS))&&($HTTP_SESSION_VARS != '') echo "hello"; It doesn't throw an error it just doesn't display anything However, this works: session_start();...
1
by: st | last post by:
Hi, I'm using xmlDocument.Save(xmlTextWriter) to create an Excel-readable file. All works well, except where I've replaced the carriage return chars in the .innertext to XML-compliant " "; It...
6
by: DAMAR | last post by:
between STAThread and MTAThread?
1
by: Richard | last post by:
Does it make sense to use <STAThread()> Public Sub Main( what's the default threading attribute for VB .NET Thanks in advance Richard
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
2
by: arnuld | last post by:
on page 29, section 1.9 Character Arrays, i see an example: /* getline: read a line into s, return length */ int getline(char s,int lim) { int c, i; for (i=0; i < lim-1 &&...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
0
by: sfbaytimes | last post by:
I am working on a C# project which uses some DCOM components that work in multithreaded mode. So using works for this reason. However, this disables some other user interface components which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.