Thread: STAThread

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    51
    Rep Power
    12

    STAThread


    I have been looking through msdn an a few books an havent been able to find an explaination on what [STAThread] is or does.

    Can someone explain to me its purpose or link me to a site that can help. thanks
    Tewl
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    1
    Rep Power
    0
    I was looking for the same thing, and your post came out first on my Google search. Anyway, Apparently it is setting a the attribute for the Main method that defines the threading model for the form. I found this in VS.Net Help under "Form Class".

    HTH,
    Tore.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0
    Say you have a program that has a function that does some intesive stuff, such as a recursive math equation or a fibonacci etc.. and you want to update a progress bar or textbar with its progress, you'll notice that if you set a property in that functions thread it won't be received until the function has been completed, visavi no progress indicator. Thats where multithreading comes in, to do something like that you use another thread, I won't go into detail about that now.

    STAThread stands for SingleThreadApplicationThread, meaning that your application is bound to its own single thread, in effect giving it more protection from accidental screw ups and such. It is also required for certain things like Dynamic menu items to work in Win98/ME.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    1
    Rep Power
    0

    STAThread attribute


    Originally posted by Tore
    I was looking for the same thing, and your post came out first on my Google search. Anyway, Apparently it is setting a the attribute for the Main method that defines the threading model for the form. I found this in VS.Net Help under "Form Class".

    HTH,
    Tore.
    From MSDN

    Indicates that the default threading model for an application is single-threaded apartment (STA).
    ,,,
    Thread Safety
    Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

    Remarks
    Threading models only pertain to applications that use COM interop. Applying this attribute to an application that does not use COM interop has no effect.

    The startup threading model can be set to single-threaded apartment or multithreaded apartment. If it is not set, then the thread is not initialized.
    ...
    dixan

IMN logo majestic logo threadwatch logo seochat tools logo