Thread State (Thread Apartment State)

In our earlier examples we see how to create a thread entry point (ThreadStart), how to create a thread, and how to start the thread. Now lets see What is thread Apartment state?

You know that the multi-threading is handled by Operating system. Each operating system has its own implementation methods for threading. The way Microsoft windows operating system handles the thread is different than Sun OS or Unix favors. I am not going to worry about how other operating system handles the threading as they are not my specialization area. Lets see what Windows offers us.

What is Apartment?
An apartment is place where objects live and communicate. Or you can say the objects are grouped into apartments. (Multi story apartment building consists one room flat :) ). Only the thread in that particular apartment can use the objects in that apartment. One process can have multiple apartments.

  1. Single Thread - Every process will have atleast one thread (Main thread or Primary thread). So when you create an application the Main() method (in console and winforms applications) spans a thread and execute your thread. (You are not creating and starting the thread where the framework does you)
  2. Multi-Thread
    • Single Threaded Apartment (STA) - Only one thread (single thread) will run at a time. Since only one thread is running and accessing your objects there will not be synchronization issues. When the message is passed to the object it will be queued in the Windows Message. (Dont worry we will see how that happens in our latter articles. But just keep in mind only one thread accessing all the objects in the Single Thread Apartment model.
    • Multi Threaded Apartment (MTA) - There are multiple threads in that apartment. So any thread can access any of the objects in the Apartment. (I mean all the objects are shared across the threads). So there will be synchronization issues.So the objects should handle the synchronization.

Let's stop the theory here and play with the code. Let's introduce couple of additional lines in our previous example.


MyClass obj = new MyClass();
ThreadStart method = new ThreadStart(obj.MyMethod);
Thread thread = new Thread(method);
Console.WriteLine(thread.GetApartmentState().ToString());
thread.Start();
Console.WriteLine(thread.GetApartmentState().ToString());

The output will be

Unknown
MTA

  • Before the thread starts the thread Apartment State is Unknown.
  • Once the thread is started then by default it takes "MTA" as thread apartment type. So default thread apartment type in .net is MTA

Lets set the Apartment State.


ThreadStart method = new ThreadStart(obj.MyMethod);
Thread thread = new Thread(method);
Console.WriteLine(thread.GetApartmentState().ToString());
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Console.WriteLine(thread.GetApartmentState().ToString());

Run it and see what happens. Lets keep exploring further details in upcoming articles.

No comments:

Post a Comment