[STAThread] is required if your app is run from IE, correct?  If so, is MS putting out 
a browser that doesn't require this?

-----Original Message-----
From: Ian Griffiths [mailto:[EMAIL PROTECTED]]
Sent: Monday, May 06, 2002 7:06 PM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Multithreaded v. Singlethreaded


The [STAThread] attribute is probably a strong contender for 'most
misunderstood feature of a typical .NET project'.  It doesn't do what you
think it does.

If you aren't using COM interop for anything, then this attribute is
irrelevant.  It only comes into play the first time your thread needs to do
something with COM - it will determine whether your thread joins an STA or
the MTA.  (I.e. will it call OleInitialize[1] or CoInitializeEx(NULL,
COINIT_MULTITHREADED)?)

This attribute has no bearing on whether your component is 'single threaded'
or 'multi-threaded'.  If some piece of code gets hold of a reference to one
of your components objects and they start calling into it from multiple
threads, then it had better be multithreaded - unlike in COM, the .NET
runtime will offer no automatic threading protection to your object.


The standard approach to multithreading is to document your class's support.
If you look at the .NET framework class library documentation you will see
that the majority of classes are documented as being unsafe at the instance
level (i.e. given a single instance of an object, clients must *not* try to
use that object simultaneously from multiple threads), but safe at the class
level (i.e. you can use the class simultaneously from multiple threads, just
not invidivual instances).  In other words, most of the FCL classes protect
their static fields, but not their per-instance fields.

None of this is automatic.  If you want your objects to be useable in a
multithreaded environment, you have to write all of the locking code
yourself.  But there is no declarative way of advertising this - you just
have to document it.



[1] I'm assuming it must call OleInitialize when in STA mode, otherwise
stuff like OLE drag and drop wouldn't work with Windows Forms applications.


--
Ian Griffiths
DevelopMentor

----- Original Message -----
From: "David B. Bitton" <[EMAIL PROTECTED]>


> I'm guessing that [STAThread] is marking to componenet as
> Single Threaded Architecture.  How can I make my dll
> multilthreading, or free threaded/thread neutral, so I can take
> advantge of MTS object pooling?

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to