STA and message pump RRS feed

  • Question

  • Hi,

    I have a WCF service which use STA com component,i need to create a pool of STA to host the com components

    in .net should i implement a message pump just like we did in the old days of COM?

    Thanks in advance

    Sunday, November 8, 2009 2:02 PM


All replies

  • Hi,

    I'm not 100% sure on this, but I think it's good enough to just mark your entry method with the STAThread attribute, i.e


    static void Main(string[] args)

    Monday, November 9, 2009 1:45 AM

  • Thanks,

    As far as i know this is only equal to  Thread.CurrentThread.ApartmentState = ApartmentState.STA
    And will not run a message loop
    Monday, November 9, 2009 4:39 AM
  • Quite possibly, but I'm not sure a message loop is required. I believe if you are hosting your WCF services in a Windows Service project then no message loop is required (either that, or one is provided for you by the .Net framework somwhere).

    If a message loop is required, you could try Application.Run which provides a message loop for you (you may need a reference to the System.Windows.Forms assembly to get access to the Application object... you can reference that from most project types fine, even if it's not a Win Forms project).
    Monday, November 9, 2009 8:29 AM
  • WCF does not require a message loop, but STA COM objects do.

    You will need a message loop. The best way to get one depends on how you're hosting your WCF service.

    Programming blog:
      Including my TCP/IP .NET Sockets FAQ

    Microsoft Certified Professional Developer
    • Marked as answer by אורצ'וק Monday, November 9, 2009 1:06 PM
    Monday, November 9, 2009 1:03 PM

  • STA Threads must have a message loop in order to synchronize-serialize and dispatch method calls to the com component
    Monday, November 9, 2009 1:06 PM
  • If you are going to create your own threadpool, use Thread.Join(1) where your threads spin, it is a pumping wait and takes care of everything for you.

    Read this article in its entirety.


    Wednesday, December 9, 2009 8:29 AM