STA Threading Model

 < Day Day Up > 



An STA thread apartment works using a concept called Object-per-Client model, meaning the code that creates the STA thread apartment owns its threads. There will only be one thread in any apartment as shown in Figure 1.

click to expand
Figure 1

In STA threading, all the calls to a thread will be placed in a queue and the calls will be processed one by one. Therefore, the STA thread will never execute multiple methods simultaneously. STA threads have their own private data and they don't share data between threads. This makes the threading model safe and avoids any data corruption and synchronization problems. However, this does restrict the options available to the developer, and performance suffers, as data has to be copied with every thread created.

As you can see from the diagram, AppDomain X has two STA threads, X and Y, running inside, and each of the STA Apartments has only one thread. The term Thread Affinity is used when defining the relationship between the thread and the code that creates the thread. When a call is made to an STA apartment thread, then calls between the caller and the thread are handled by the contexts in the AppDomain, and the contexts maintain the thread affinity.

If your managed application is going to use unmanaged legacy COM components, then it is very important to know the threading model of the COM components before accessing them. If you don't mark the correct threading mode in your application, there could be some unexpected bugs and catastrophic errors in your application. The threading model information can be found in the registry under the HKEY_CLASSES_ROOT\CLSID\{Class ID of the COM component} \InProcServer32 key.

If you want to specify that you are using the Apartment Threading model, then apply the STAThreadAttribute attribute on the Main () method.

    [STAThreadAttribute]    static void Main()    {      ...    } 

This attribute should only be used if we're trying to access legacy STA components from the managed code. Otherwise, mark the Main() method as MTAThreadAttribute:

    [MTAThreadAttribute]    static void Main()    {      ...    } 

The same principal applies for ASP.NET applications. If your ASP.NET page is accessing an STA COM component, then you have to use the ASPCompat directive at the top of the ASP.NET page:

    <%@ Page AspCompat="true" %> 

By default, all the ASP.NET pages are multithreaded and when we use the AspCompat directive, the ASP.NET page is marked as STA. This will ensure the ASP.NET page is compatible with the threading model of a COM component.

start sidebar

When you mark the ASP.NET page to run under the STA threading model, the performance of the application may suffer.

end sidebar

Note 

If you are using VB.NET then you can use CreateObject statement to instantiate COM objects. Since C# doesn't allow late binding, the only way to call COM objects in late binding mode is to use reflection.



 < Day Day Up > 



C# Threading Handbook
C# Threading Handbook
ISBN: 1861008295
EAN: 2147483647
Year: 2003
Pages: 74

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net