none
what is accomplished by decorating a C-Sharp Main Subroutine with MTAThread vs. STAThread. RRS feed

  • Question

  • Hi,

    Can you please tell what is accomplished by decorating a C-Sharp Main Subroutine with MTAThread vs. STAThread.

    I have read couple C-Sharp Multithreading books, but I did not find any explanation of applicability of STAThread or MTAThread in C sharp code.

    Why are such attributes needed in a C-sharp code in the first place ?

    I had a Console Application where its Main was decorated with STAThread by default. Everything worked without problem. Then I added a WCF service, which services a separate GUI Client application. When I use the GUI, my Console application gives a ContextSwitchDeadlock error. The only way I can get rid of the error is by marking the MAIN MTAThread. But I do not why the error goes away. 

    Also, My Console Application does use a COM Based service, but I do not know if that has a bearing on this problem or not.

    Help appreciated. 


    Thank you 
    • Edited by Diana4 Wednesday, November 4, 2009 9:12 PM
    Wednesday, November 4, 2009 8:25 PM

Answers

  • STAThread indicates that the COM thread model for your application is SingleThreadedApartment Model. MTAThread sets the threading model to MultiThreadedApartment model.

    MSDN has a pretty good explanation of the attribute : http://msdn.microsoft.com/en-us/library/system.mtathreadattribute.aspx

    Your WCF application was deadlocking because your forcing an inherently multi threaded operation into a SingleThreadedApartment state.

    Take note of the changes in framework versions regarding this topic as noted in the linked MSDN entry.
    • Marked as answer by eryang Tuesday, November 10, 2009 9:29 AM
    Thursday, November 5, 2009 2:16 AM
  • Hi,

    I don't know a lot of detail on this subject, but it is important to use the STAThread attribute if you are going to use COM components in your applications. For example, trying to host the .Net web browser control (which actually just wraps the IE COM control) can be a disaster if your application isn't set to your the STA model. I believe that is the primary purpose of these attributes, to ensure that when your code is running in certain environment or using certain classes of legacy technologies that require a particular model that it can still play nicely.

    The web browser control is actually an interesting case... from memory the Compact Framework version of the control isn't STA but rather MTA. Compact framework apps are supposed to be 'binary compatible' with the desktop version, that is, a compact framework exe will run in a desktop environment, but that is not the case when a Windows Form hosts a web browser control as the desktop system switches to the desktop version of the control that requires a different threading model and then the system crashes. As far as I know, there still isn't a work around for that.
    • Marked as answer by eryang Tuesday, November 10, 2009 9:29 AM
    Wednesday, November 4, 2009 8:41 PM

All replies

  • Hi,

    I don't know a lot of detail on this subject, but it is important to use the STAThread attribute if you are going to use COM components in your applications. For example, trying to host the .Net web browser control (which actually just wraps the IE COM control) can be a disaster if your application isn't set to your the STA model. I believe that is the primary purpose of these attributes, to ensure that when your code is running in certain environment or using certain classes of legacy technologies that require a particular model that it can still play nicely.

    The web browser control is actually an interesting case... from memory the Compact Framework version of the control isn't STA but rather MTA. Compact framework apps are supposed to be 'binary compatible' with the desktop version, that is, a compact framework exe will run in a desktop environment, but that is not the case when a Windows Form hosts a web browser control as the desktop system switches to the desktop version of the control that requires a different threading model and then the system crashes. As far as I know, there still isn't a work around for that.
    • Marked as answer by eryang Tuesday, November 10, 2009 9:29 AM
    Wednesday, November 4, 2009 8:41 PM
  • STAThread indicates that the COM thread model for your application is SingleThreadedApartment Model. MTAThread sets the threading model to MultiThreadedApartment model.

    MSDN has a pretty good explanation of the attribute : http://msdn.microsoft.com/en-us/library/system.mtathreadattribute.aspx

    Your WCF application was deadlocking because your forcing an inherently multi threaded operation into a SingleThreadedApartment state.

    Take note of the changes in framework versions regarding this topic as noted in the linked MSDN entry.
    • Marked as answer by eryang Tuesday, November 10, 2009 9:29 AM
    Thursday, November 5, 2009 2:16 AM
  • Thank you for your response.
    Tuesday, November 10, 2009 11:21 PM
  • What is single threaded apartment model then ?!!!!
    - Rajesh K http://thoughtsontechies.blogspot.com/
    Thursday, April 7, 2011 10:17 AM