[STAThread]???

S

SSP

I don't get it??

Until few minutes I debugged my forms and it worked like a breeze...no
problems.

Then suddenly I started getting a totally unrelated error message like:

"DragDrop registration failed"

After a fuming for a few minutes I discovered that it needed "[STAThread]"
stuff ahead of calling the Main method.

I am new to windows forms (primarily a web developer)...I don't understand.
Why did it start to appear all of a sudden? What the h*** does it do??

SSP
 
J

Jon Skeet

SSP said:
I don't get it??

Until few minutes I debugged my forms and it worked like a breeze...no
problems.

Then suddenly I started getting a totally unrelated error message like:

"DragDrop registration failed"

After a fuming for a few minutes I discovered that it needed "[STAThread]"
stuff ahead of calling the Main method.

I am new to windows forms (primarily a web developer)...I don't understand.
Why did it start to appear all of a sudden? What the h*** does it do??

It basically makes sure that the COM apartment threading model is the
Single Threaded Apartment model. The first time any method with the
[STAThread] or [MTAThread] attribute is called on any particular
thread, that threading model is set up for that thread. Lots of
controls require the STA mode, so basically all Windows Forms programs
are likely to have that attribute on their Main method (or somewhere
similar).

I don't know many details about this - such as whether you can have
some threads in STAs and others in MTAs.
 
P

Phil Wright

All windows controls have thread affinity and so must be called from the
same thread in which they were created. Therefore when you register a
control for drag and drop it will throw an exception if it is in the MTA
(multi threaded apartment) because in the MTA the call could be coming from
any random RPC thread. Therefore you need to mark your main thread as STA as
thats where you are creating your GUI controls. If you do not mark your
thread with STAThread then the first time it needs to use COM is will
default to adding itself to the MTA.

Hope that helps,
Phil.
DotNetMagic
 
J

Joe White

Hmm, good info -- I never knew exactly what that was for either.

So if I wanted to start a secondary UI thread with its own message pump,
and if that secondary thread used COM stuff like the clipboard or any
common controls, then I would probably need [STAThread] somewhere along
the line, right? Would it be best to put that attribute on the method
that's referenced by the ThreadStart or WaitCallback delegate?
 
?

=?ISO-8859-1?Q?Christian_Fr=F6schlin?=

Joe said:
So if I wanted to start a secondary UI thread with its own message pump,
and if that secondary thread used COM stuff like the clipboard or any
common controls, then I would probably need [STAThread] somewhere along
the line, right? Would it be best to put that attribute on the method
that's referenced by the ThreadStart or WaitCallback delegate?

I think you would set Thread.ApartmentState before starting the thread
 

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