473,762 Members | 5,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21323

"Brett" <no@spam.net> wrote in message
news:OT******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP10.phx.gbl...

"Brett" <no@spam.net> wrote in message
news:OT******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP10.phx.gbl...
"Brett" <no@spam.net> wrote in message
news:OT****** ********@TK2MSF TNGP09.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
AppartmentSta te 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.co m>
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******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP10.phx.gbl. ..

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

"Brett" <no@spam.net> wrote in message
news:OT******** ******@TK2MSFTN GP09.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.Manageme nt, System.Director yServices,
System.Enterpri seServices 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.N ET 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2054
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(); if (isset($HTTP_SESSION_VARS))
1
3980
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 gets changed to "&amp;#10" and doesn't render new lines in the Excel sheet. Can anyone help? Many thanks,
6
1456
by: DAMAR | last post by:
between STAThread and MTAThread?
1
1217
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
10117
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) ..... // xsl contents abc.aspx?p1=v1&amp;p2=<xsl:value-of select="$v2" />
7
4628
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 BeautifulSoup to clean those up? There are various parsing options related to "&" handling, but none of them seem to do quite the right thing. If I write the BeautifulSoup parse tree back out with "prettify", the loose "&" is still in there. So...
2
1981
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 && (c=getchar())!=EOF && c!='\n'; ++i) s = c; if (c == '\n') {
23
4371
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 non-two's-complement machine. Just wondering.) -- Hallvard
0
1240
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 require it to be STA. Does anyone have any recommendations how can one mix the two modes to makes all the components work? All help is greatly appreciated. Thanks, SF
0
9378
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10137
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9927
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9812
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3914
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.