By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,677 Members | 1,218 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,677 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Dec 2 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<ga********@myway.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 2 '06 #2

P: n/a
On 2 Dec 2006 14:31:55 -0800, ga********@myway.com wrote:
>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
Dec 3 '06 #3

P: n/a
ga********@myway.com wrote:
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!

--
Dr Jon D Harrop, Flying Frog Consultancy
Objective CAML for Scientists
http://www.ffconsultancy.com/product...ex.html?usenet
Dec 3 '06 #4

P: n/a
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

<ga********@myway.com>
??????:11**********************@n67g2000cwd.google groups.com...
>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.

Dec 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.