> Hi, guys
> I have a COM interop object (named as "PlantWrap") which is written with
[quoted text clipped - 22 lines]
> And the call to Plantwrap failed with error: -2147221008, 'CoInitialize has
> not been called.'.
try to call
Thread.SetApartmentState(ApartmentState.STA);
before call Thread.Start().
In Dotnet2, the default apartmentstate is "unknown" of a new thread,
while the default apartmentstate is "STA".
Hence, you must set ApartmentState before start a new thread in Dotnet2.

Signature
Jacky Kwok
jacky@alumni_DOT_cuhk_DOT_edu_DOT_hk
jacky@compose_DOT_com_DOT_hk
jacky kwok - 21 Sep 2006 07:25 GMT
> In Dotnet2, the default apartmentstate is "unknown" of a new thread,
> while the default apartmentstate is "STA".
> Hence, you must set ApartmentState before start a new thread in Dotnet2.
Some correction,
> In Dotnet2, the default apartmentstate is "unknown" of a new thread,
> while the default apartmentstate is "STA".
I mean "while DOTNET 1.x the default apartmentstate is 'STA'."

Signature
Jacky Kwok
jacky@alumni_DOT_cuhk_DOT_edu_DOT_hk
jacky@compose_DOT_com_DOT_hk
YeJianWei - 21 Sep 2006 07:38 GMT
Thank you!
I found that this problem could be resolved by calling
"Thread.CurrentThread.ApartmentState = ApartmentState.STA" at the entry of
the working thread.
But I'm still confused about the STAThread attribute, I had read the MSDN
and it recommend me to use this attribute instead of calling the
ApartmentState directly,
so why the attribute not work? Can anybody give me an answer?
BTW: I do use the dotNet Framework 1.1
> > Hi, guys
> > I have a COM interop object (named as "PlantWrap") which is written with
[quoted text clipped - 32 lines]
> while the default apartmentstate is "STA".
> Hence, you must set ApartmentState before start a new thread in Dotnet2.
Mattias Sjögren - 21 Sep 2006 21:37 GMT
>so why the attribute not work? Can anybody give me an answer?
It only works when placed on the startup method (Main) and only
affects the applications startup thread. It's needed because by the
time your managed code in Main runs, the CLR has already initialized
COM on that thread and therefore it's too late to set the
ApartmentState. For all other threads, that's what you should do.
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.