STA vs MTA

  • Thread starter Thread starter Ashish Sheth
  • Start date Start date
A

Ashish Sheth

HI Gurus,
Basically I want to know what is STA and what is MTA? what are the
advantages and disadvantages of each one over the other?
Also when we are creating any console application or windows application on
the VisualStudio automatically aplies STAAttribute to the Main function?
why? why is it necessary?

thanks and regards,
Ashish Sheth
 
Ashish Sheth said:
HI Gurus,
Basically I want to know what is STA and what is MTA? what are the
advantages and disadvantages of each one over the other?
Also when we are creating any console application or windows application
on
the VisualStudio automatically aplies STAAttribute to the Main function?
why? why is it necessary?

They specify COM threading, basically. STAAttribute is required because some
of the windows form controls are wrappers around COM objects which have to
be run under STA. This article explains it in some of its gory detail:
http://blogs.msdn.com/cbrumme/archive/2004/02/02/66219.aspx
 
These are threading attributes. In the days of COM, (do I sound like a vet. or what) these were specified in registry to let the COM runtime know that either you would like your own thread to run in (when somebody creates you) and let others marshal any requests into that thread [this is the Single Therad Apartment model] or you are a good threading citizen who can be created in any thread and any calls to you can be directly sent to you and you will manage any "threading" related issues (such as locking down any state vars, using Critical sections for the code which modify those vars etc) [This is the Multi Threaded Apartment, also known as Free threaded]. There was a model called TNA (Thread neutral Apartment) introduced in Win2k which was context sensitive but thread agnostic.
Anyway, nowadays, this is primarily tacked on at the beginning of an Application run in order to make sure that some of the controls behave correctly. [Any UI control is effectively a STA object since you only have one thread where windows message are pumped].

check out Chris Brumme's blog entry pointed to by the other poster. That will teach you more than you will ever want to know about such nasty things.
 
Back
Top