This tutorial is about another subject for beginners named:
1 | [STAThread] |
I saw many questions on the web about this subject and this is the reason for this tutorial.
The [STAThread] is a carry-over from Microsoft’s “Component Object Model”, known as COM.
Because COM provided both single-threaded apartment (STA), and multi-threaded (MTA) programming models then that needs some changes.
The .NET CLR uses COM Interop between the .NET Types and COM facilities and object and this is the main reason why is the need for this attribute.
The states in managed threads set a threading model on your application by setting the attribute [STAThread] on the main() method.
You will also need to add the STAThread attribute to the Main method for the form to run.
NOTE: The implementation of the NET on Linux distros let’s use it, see the next example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [mythcat@desk CSharpProjects]$ cat STAThread.cs using System; class Hello { /// <summary> /// The main entry point for the application /// </summary> [STAThread] public static void Main(string[] args) { System.Console.Write("Hello, World!\n"); } }[mythcat@desk CSharpProjects]$mcs STAThread.cs [mythcat@desk CSharpProjects]$ mono STAThread.exe Hello, World! |