I am learning C# 3.5 and I want to know what [STAThread]
does in our programs?
3 Answers
The STAThreadAttribute
is essentially a requirement for the Windows message pump to communicate with COM components. Although core Windows Forms does not use COM, many components of the OS such as system dialogs do use this technology.
MSDN explains the reason in slightly more detail:
STAThreadAttribute indicates that the COM threading model for the application is single-threaded apartment. This attribute must be present on the entry point of any application that uses Windows Forms; if it is omitted, the Windows components might not work correctly. If the attribute is not present, the application uses the multithreaded apartment model, which is not supported for Windows Forms.
This blog post (Why is STAThread required?) also explains the requirement quite well. If you want a more in-depth view as to how the threading model works at the CLR level, see this MSDN Magazine article from June 2004 (Archived, Apr. 2009).
-
1any idea why CompactFramework doesn't support `[STAThread]` ? – bvdb Jul 28 '16 at 10:11
-
5https://stackoverflow.com/questions/4154429/apartmentstate-for-dummies this answer is pretty understandable for mortals like me. Added just for reference here – Barış Akkurt Feb 22 '19 at 12:51
It tells the compiler that you're in a Single Thread Apartment model. This is an evil COM thing, it's usually used for Windows Forms (GUI's) as that uses Win32 for its drawing COM for drag and drop COM components (thanks @AnthonyWJones), which is implemented as STA. If you are using something that's STA model from multiple threads then you get corrupted objects.
This is why you have to invoke onto the Gui from another thread (if you've done any forms coding).
Basically don't worry about it, just accept that Windows GUI threads must be marked as STA otherwise weird stuff happens.

- 28,526
- 15
- 68
- 103
-
5STAThread has nothing to do with the requirement to invoke the main thread when accessing GUI. This is simply due to the nature of the Windows message pump, and cannot be avoided more generally in multithreaded applications. – Noldorin Sep 01 '09 at 07:42
-
4Really, it's only about dealing with COM components such as OS dialogs and third-party components. – Noldorin Sep 01 '09 at 07:42
-
4Win32 carries no concept of threading apartments, its COM which introduces the concept. COM "re-tasks" what was an entirely thread agnostic system (the windows message pump) as a means to synchronize/serialise code execution in COM apartments. – AnthonyWJones Sep 01 '09 at 08:08
-
2Just accept that windows gui theads must be marked as STA otherwise weird stuff happens. :)))))) – Nipuna Mar 08 '11 at 05:56
-
1@Noldorin "requirement to invoke the main thread" - this is not technically a *requirement*. Cross-thread exceptions do not occur outside of debugger. Ref: https://stackoverflow.com/questions/3972727/why-is-cross-thread-operation-exception-not-thrown-while-running-exe-in-bin-debu. Not saying you should not solve this problem however! – Shiv Oct 24 '14 at 02:16
-
Thanks for having written what STA actually stands for, which is not the case in the accepted answer – Couitchy Jun 27 '21 at 19:44
-
It's all about removing locks where you can to optimise throughput. STA is a great way to understand why old windows apps would "freeze", because something would block the single GUI drawing thread and stop all messages being processed by the pump. @AnthonyWJones has clarified it's COM not Win32 doing it to do the serialization (handling locks and concurrency issues) – Spence Jul 12 '21 at 06:00
The STAThreadAttribute marks a thread to use the Single-Threaded COM Apartment if COM is needed. By default, .NET won't initialize COM at all. It's only when COM is needed, like when a COM object or COM Control is created or when drag 'n' drop is needed, that COM is initialized. When that happens, .NET calls the underlying CoInitializeEx function, which takes a flag indicating whether to join the thread to a multi-threaded or single-threaded apartment.
Read more info here (Archived, June 2009)
and