TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

What is STAThread Attribute?

Attribute [STAThread] is a mandatory attribute for all windows forms application. STAThread attribute need to be applied to set the current thread apartment state as single threaded. This means that the COM threading model for the application is single-threaded apartment.
You must have noticed that entry point Main() method in program.cs class of all .NET windows forms applications are always decorated with the attribute STAThread. What is this attribute and why is it needed?
Attribute [STAThread] is a mandatory attribute for all windows forms application. STAThread attribute need to be applied to set the current thread apartment state as single threaded. This means that the COM threading model for the application is single-threaded apartment.

The Innovators: How a Group of Inventors, Hackers, Geniuses and Geeks Created the Digital Revolution- A must Read

This attribute specifies the communication mechanism between the current thread and other threads that may want to talk to it via COM.
Windows Forms applications may use COM interop in order to communicate with operating system components such as File Dialogs,Clipboard etc. STAThread attribute
Apartment style is mandatorily needed to be declared for windows forms appliations.If not declared it may be that some other component which communicate your windows application will declare an  apartment style for your windows application improperly.

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading