C#

What is STAThread Attribute? | What Does [STAThread] Do?

C# STAThread Attribute

In this post we will discuss the the purpose of STAThread attribute in Main() method of the C# program.
[STAThread]
static void Main()

You must have noticed the above-given entry point Main() method in the program.cs class of all  .NET windows forms applications are always decorated with the attribute STAThread.

What is [STAThread] Attribute?  What Does [STAThread]  Mean?

Attribute [STAThread] is a mandatory attribute for all windows forms application. STAThread attribute needs to be applied to set the current thread apartment state as single threaded.

This means that the COM threading model for the application is a single-threaded apartment.

What Does [STAThread]  Do?

STAThreadAttribute is carried over from COM.  STAThread 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. If our application is not able to control the state of the apartment of the current running thread, it should start up a new thread.

Apartment style declaration is mandatory for Windows Forms applications.

[STAThread]
static void Main()

If apartment style not declared, it may be that some other component which communicates your windows application will declare an apartment style for your windows application improperly.

There is another way to set a thread’s apartment state as STA like below, but not required for entry point Main() since the best easy default way is to use STAThreadAtrribute.

// A simple console program showing to Set ApartmentState as STA 

using System;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
    }
}

For methods other than entry point Main(), Start new thread and set the thread’s apartment state to STA as shown

thread.SetApartmentState(ApartmentState.STA);
thread.Start();

Situations to Use STAThreadAttribute

Using the STAThreadAttribute  to set the threading model to single-hreaded apartment is useful in the following situations:

The application you’re working on uses the Windows Forms programming. Single-threading is required for Windows Forms apps that connect with Windows system components like the Clipboard or Windows standard dialogue boxes.

Also, if they utilize system functions like drag-and-drop. Projects in C# using the Windows Forms Application template have the STAThreadAttribute attribute automatically added to them.

But in Visual Basic, it is not needed the attribute because Visual Basic has a single-threaded apartment model by default.

Your C# software calls a Visual Basic library which internally depends on COM interoperability to function properly. You should utilise the STAThreadAttribute attribute to alter your app’s threading model to single-threaded since that is the default threading model for Visual Basic.

In cases when there are COM components that employ the single-threaded apartment model are called by your application.

FAQ on C# STAThread

Why Windows Forms Entry Points are Marked with STAThread by Default?

STAThread attribute will making it a Single Thread Apartment model. Hence, all multiple-threaded calls will be marshalled to this thread before they are called. The reason for this is because Windows Forms employs various OLE calls that must be made from the thread that initialized OLE.

The Windows components may not work correctly if this attribute is missing from the entry point of any winforms application.

Can we have Multiple [STAThread] in a C# Class Library?

Place [STAThread] attribute only once in a library, and only above the entry point method such as Main().

Summary

In this post we covered the STAThreadAttribute which specifies that it is a single-threaded apartment. Hope you found this article useful.

Rajeev

Recent Posts

OWIN Authentication in .NET Core

OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…

2 years ago

Serializing and Deserializing JSON using Jsonconvertor in C#

JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…

2 years ago

What is CAP Theorem? | What is Brewer’s Theorem?

The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…

2 years ago

SOLID -Basic Software Design Principles

Some of the Key factors that need to consider while architecting or designing a software…

2 years ago

What is Interface Segregation Principle (ISP) in SOLID Design Principles?

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…

2 years ago

What is Single Responsibility Principle (SRP) in SOLID Design Priciples?

The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…

2 years ago