none
STA in AppDomain RRS feed

Answers

  • AppDomains do not have an apartment mode, threads do.  Neither do AppDomains have their own threads.  The same thread that calls into the AppDomain runs the code within the AppDomain.

    First you need to create a Thread (unless you are willing to tie up the current thread and the apartment state has not yet been set on the current thread -- not likely).  Then, in the ThreadStart method for that thread, call Thread.SetApartmentState.  Afterwards, still within the ThreadStart, execute code using the AppDomain (such as via AppDomain.ExecuteAssembly or other means).  If you need to wait for this execution code to complete, your other thread can call Thread.Join on the new thread.
    • Marked as answer by Yaron Naveh Wednesday, January 14, 2009 11:01 AM
    Tuesday, January 13, 2009 11:57 PM
  • The STA threading model ensures that method calls on an object are automatically marshaled to the apartment that created the object.  COM implements this marshaling with the message loop, it is the one and only place in Windows where it is safe to inject a method call into a thread.  Windows Forms does the same thing when it implements Control.Invoke().  Without a message loop, your program will typically deadlock, waiting for method call to complete that never gets dispatched.  That commonly happens in code you didn't write, when dealing with the clipboard or using one of the shell dialogs (OpenFileDialog etc) for example, or the component relying on the marshaling itself, making the deadlock very hard to diagnose.

    The COM threading model is pretty notorious, it is helping too much.  Taking care of stuff automatically in 90% of the cases, but giving you a major headache in the remaining 10.  Pumping messages in an appdomain gets you close to that red zone.  I've never tried it, please post back when you get it working to confirm that it works without problems.

    Hans Passant.
    • Marked as answer by Yaron Naveh Wednesday, January 14, 2009 10:10 PM
    Wednesday, January 14, 2009 12:19 PM
    Moderator

All replies

  • AppDomains do not have an apartment mode, threads do.  Neither do AppDomains have their own threads.  The same thread that calls into the AppDomain runs the code within the AppDomain.

    First you need to create a Thread (unless you are willing to tie up the current thread and the apartment state has not yet been set on the current thread -- not likely).  Then, in the ThreadStart method for that thread, call Thread.SetApartmentState.  Afterwards, still within the ThreadStart, execute code using the AppDomain (such as via AppDomain.ExecuteAssembly or other means).  If you need to wait for this execution code to complete, your other thread can call Thread.Join on the new thread.
    • Marked as answer by Yaron Naveh Wednesday, January 14, 2009 11:01 AM
    Tuesday, January 13, 2009 11:57 PM
  • Don't forget to pump a message loop, a hard requirement for an STA.
    Hans Passant.
    Wednesday, January 14, 2009 1:18 AM
    Moderator
  • nobugs - can you explain what do you mean?
    http://webservices20.blogspot.com/
    WCF Security, Performance And Testing Blog
    Wednesday, January 14, 2009 11:01 AM
  • The STA threading model ensures that method calls on an object are automatically marshaled to the apartment that created the object.  COM implements this marshaling with the message loop, it is the one and only place in Windows where it is safe to inject a method call into a thread.  Windows Forms does the same thing when it implements Control.Invoke().  Without a message loop, your program will typically deadlock, waiting for method call to complete that never gets dispatched.  That commonly happens in code you didn't write, when dealing with the clipboard or using one of the shell dialogs (OpenFileDialog etc) for example, or the component relying on the marshaling itself, making the deadlock very hard to diagnose.

    The COM threading model is pretty notorious, it is helping too much.  Taking care of stuff automatically in 90% of the cases, but giving you a major headache in the remaining 10.  Pumping messages in an appdomain gets you close to that red zone.  I've never tried it, please post back when you get it working to confirm that it works without problems.

    Hans Passant.
    • Marked as answer by Yaron Naveh Wednesday, January 14, 2009 10:10 PM
    Wednesday, January 14, 2009 12:19 PM
    Moderator