What is [STAThread] what does it mean? what does it do?

G

garyusenet

I am at a loss with this. I tried to go back to basics, and start
learning all i didn't understand, starting at the top of the code file
generated by VS. But I can't seem to get any sort of start on this
problem, anything I read on the NET goes too far above my head to be of
any use.

For instance typical explanations say things like...

'The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed'

What on this earth is a Single Threaded COM Apartment? I won't continue
quiting sentences of explanation that I don't understand.

But could someone please attempt to tell a beginner that does not know
anything about 'threading' what this instruction actually does?

Thankyou very much,

Gary.
 
J

Jon Skeet [C# MVP]

I am at a loss with this. I tried to go back to basics, and start
learning all i didn't understand, starting at the top of the code file
generated by VS. But I can't seem to get any sort of start on this
problem, anything I read on the NET goes too far above my head to be of
any use.

For instance typical explanations say things like...

'The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed'

What on this earth is a Single Threaded COM Apartment? I won't continue
quiting sentences of explanation that I don't understand.

But could someone please attempt to tell a beginner that does not know
anything about 'threading' what this instruction actually does?

Probably the best way of reading up on this is to do a Google search
for "COM threading models" and read some explanations on the web.
Alternatively, buy one of the many, many COM books available.
 
O

Otis Mukinfus

I am at a loss with this. I tried to go back to basics, and start
learning all i didn't understand, starting at the top of the code file
generated by VS. But I can't seem to get any sort of start on this
problem, anything I read on the NET goes too far above my head to be of
any use.

For instance typical explanations say things like...

'The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed'

What on this earth is a Single Threaded COM Apartment? I won't continue
quiting sentences of explanation that I don't understand.

But could someone please attempt to tell a beginner that does not know
anything about 'threading' what this instruction actually does?

Thankyou very much,

Gary.

A search for STAThread returns 211,000 results from Google.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
J

Jon Harrop

I am at a loss with this. I tried to go back to basics, and start
learning all i didn't understand, starting at the top of the code file
generated by VS. But I can't seem to get any sort of start on this
problem, anything I read on the NET goes too far above my head to be of
any use.

For instance typical explanations say things like...

'The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed'

What on this earth is a Single Threaded COM Apartment? I won't continue
quiting sentences of explanation that I don't understand.

But could someone please attempt to tell a beginner that does not know
anything about 'threading' what this instruction actually does?

No idea. I wish I knew what STAThread does. Apparently you need to declare
it for all WinForms programs.

My guess is that it is something to do with "message loops" getting confused
if they are used concurrently.

I think this underpins the main problem I've been having with F#. Unlike C#,
F# provides an interactive mode where you can type code and have it
executed immediately. Although it is supposed to make WinForms programming
easier (because you can play with your windows in real time) it has some
problems.

For example, when a file dialog box has been created the WinForms part of
your program can hang while the F# interactive mode waits for more input.
In order to use the dialog box you must enter something into the F#
interactive mode so the dialog box will get some CPU time.

There must be some magic incantation to spawn a new message loop for another
thread and then have the whole of my GUI work done in another thread, so it
won't block on the interactive mode's input. I've no idea what that is
though.

I'd love it if anyone here could shed some light on this or point me to
something more specific than google.com.

Best of luck!
 
J

Justin li

Single Thread Apartment vs MultiThread Apartment?

Correct: With the STAThread attribute, you will be interacting with COM
processes in a "Single Threading Apartment" model. Without it, you will be
interacting with COM processes in the "Multiple Threading Apartment" model.
so why do I need it....or why would I want it at some point?

You may want to interact with a COM process in a MTA model for performance
reasons. You may want to interact with a COM process in a STA model because
of a design requirement. For example, to use the Windows clipboard
(System.Windows.Forms.Clipboard) you must be calling from a thread running
in a STA. If the calling thread was started by your application you can set
the ApartmentState (System.Threading.ApartmentState) before starting, but if
you want to use the clipboard from your application's main thread, you need
to use the System.STAThread attribute on your Main method.
why does Main( ) only function as an entry point when it is declared
static?

The simple answer is that is just the way that Microsoft designed the
language. One way you can look at this though, is there should only be 1
"instance" of your Main method - the main method has nothing to do with any
specific instances of the class it is defined in, and should therefore be
static. In my opinion it might have been a good idea to give the Main method
a property similar to a static contructor where it is executed once, and
only once. Anyway, because the Main method is static, you can execute your
program without having to create any arbitrary objects.

For detail to see:
http://www.codeproject.com/com/CCOMThread.asp

Justin Li
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top