OK, What is with the statements enclosed inside [] operator regarding threading?

I keep looking around to try and figure this out. I keep seeing code with something like [STAThread] on a line by itself.

Now, I have figured out that it sets the type of apartmenting you can do. And you seem to use it like the using namespace statements to define if you are going to hae a sta or a mta, but where in the world does the syntax for this statement come from   Is this strictly a COM feature in which it overloaded the [] operator

Just wanted to make sure I didn't miss some new language element that I may need.

Also, I seem to have gathered that you only need to mark things as STA or MTA using this feature if you are going to be interfacing with a COM DLL If you stay in the .NET you can ignore this  



Answer this question

OK, What is with the statements enclosed inside [] operator regarding threading?

  • WojtekG

  • Antons1

    You might want to jump right into C++/CLI (2005) and avoid Managed C++ (2003) since the syntax is significantly different.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB.NET to C# Converter
    Instant VB: C# to VB.NET Converter
    Instant C++: C# to C++ Converter
    Instant J#: VB.NET to J# Converter
    Clear VB: Cleans up VB.NET code

  • arti_z

    These are Attributes. There are several kinds of attributes including those that you define yourself (look into the System.Reflection namespace). In the case of STAThread or MTAThread, the CLR will set the Apartment type of the main thread accordingly -- the same as calling Thread.SetApartmentState. In the case of STAThread, COM is initialized for the thread with a call to OleInitialize() which enables using drag-and-drop, the clipboard and other features. The best thing to do is always make a WinForms app an STAThread.


  • Yorch

    C++/CLI is VC++ 2005
    You've got it - not having to make the 2003 - 2005 transition makes your life easier.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB.NET to C# Converter
    Instant VB: C# to VB.NET Converter
    Instant C++: C# to C++ Converter
    Instant J#: VB.NET to J# Converter
    Clear VB: Cleans up outdated VB.NET code



  • intSteve

    Thank you all of you.  That helps a lot.  I thought there had to be something new added in there.  Haven't done any programming siince 98 and that was using BC++.  Visual C++ is a lot different. 
  • Dino Viehland

    I am using VC++ .NET 2005 Beta atm. Is CLI the ANSI standard or is it another flavor of VC++
  • Boltress

    This is attribute syntax.  It's the same as C#.
    Attributes are meta-data about the entity you apply the attribute to.

    Examples are:
    [Obsolete]
    [Serializable]

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB.NET to C# Converter
    Instant VB: C# to VB.NET Converter
    Instant J#: VB.NET to J# Converter
    Clear VB: Cleans up VB.NET code

  • EAdam

    Cool.  I knew I had come across that acronym several times but couldn't remember in what context.  That's one of the reasons I downloaded the beta rather than sinking money into the 2003 version. Glad I did. It is actually a little easier to understand some of the concepts as far as I am concerned.  Just means that some of the things the beta takes for granted I know leaves me a little frustrated from time to time. But it's fun in any case to get back into it.

  • OK, What is with the statements enclosed inside [] operator regarding threading?