Welcome to MSDN Blogs Sign in | Join | Help

jfo's coding

adventures in windows forms and wpf
Why is STAThread required?

Over the top of your Main method, you may have noticed this funny decoration:

[STAThread]
static void Main() {

Several people have asked, what is this and is it this actually necessary? 

When the STAThreadAttribute is applied, it changes the apartment state of the current thread to be single threaded.  Without getting into a huge discussion about COM and threading, this attribute ensures the communication mechanism between the current thread and other threads that may want to talk to it via COM.  When you're using Windows Forms, depending on the feature you're using, it may be using COM interop in order to communicate with operating system components.  Good examples of this are the Clipboard and the File Dialogs. 

Windows Forms is not supported within a MTA or free threaded apartment.  Applications using Windows Forms should always declare the apartment style they're using, as some other component could initialize the apartment state of thread improperly. 

If the application cannot control the apartment state of the current thread, it should start up a new thread.  Here's a quick example:

using System.Threading;

Thread t = new Thread(new ThreadStart(StartNewStaThread));

// Make sure to set the apartment state BEFORE starting the thread.
t.ApartmentState = ApartmentState.STA;
t.Start();

private void StartNewStaThread() {
    Application.Run(new Form1());
}

Related Documentation:
  • INFO: Calling Shell Functions and Interfaces from a Multithreaded Apartment
  • Shell does not support MTA
  • Choosing the Threading Model

    [Post 2.0 release update]
    Instead of using t.ApartmentState, use t.SetApartmentState(ApartmentState.STA);   

    If you dont want to fuss with setting it manually, you can also put the [STAThread] attribute on top of StartNewStaThread method:

    Thread t = new Thread(new ThreadStart(StartNewStaThread));
    t.Start();

    [STAThread]
    private void StartNewStaThread() {
        Application.Run(new Form1());
    }

  • Posted: Thursday, April 07, 2005 6:36 PM by jfoscoding
    Filed under:

    Comments

    Hello??? .... is this thing on?? said:

    # April 8, 2005 9:10 AM

    jfo's coding said:

    Custom PaintingPainting best practices ComboBox OwnerDrawLayoutDock layout/Using the Splitter control...
    # August 31, 2005 7:27 PM

    Shan said:

    Thread
    # April 12, 2006 5:35 AM

    Dhaval said:

    I was going to post a comment on this one but found this one..so would not need one Why is STAThread...

    # October 28, 2006 8:22 PM

    The Visual Basic Team said:

    I recently ran into an interesting threading problem that I wanted to share, so that perhaps I can save

    # March 24, 2008 3:49 PM

    The Visual Basic Team said:

    最近遭遇したスレッドの問題を皆さんにご紹介しましょう。私と同じ目に遭う人を減らせるかもしれません。 ここで、 だれ かが書いた C# アプリケーションを例とし、ここでは "DeltaEngine" と呼ぶことにします。

    # June 10, 2008 2:23 AM

    [STAThread] | keyongtech said:

    # January 21, 2009 9:32 PM
    New Comments to this post are disabled
    Page view tracker