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.