Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2006

Tip: Looking for answers? Try searching our database.

Why the "STAThread" not work

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
YeJianWei - 21 Sep 2006 07:15 GMT
Hi, guys
I have a COM interop object (named as "PlantWrap") which is written with
managed c++, the plantwrap is a wrapper for another COM based server, and the
threadmodel of that COM server is apartment.
To call the plantwrap in work thread, I have to initialize the COM library
and thread apartment, so here is my code:

//Working Thread
[STAThread]
static void ThreadProc()
{
 string x = Thread.CurrentThread.ApartmentState.ToString();
 Console.WriteLine("ThreadState = {0}", x);
 PlantWrap p = new PlantWrap;
 UINT uiResult =p.SomeFuncr(..)
 ....
 return;
}

//Main Thread
new Thread( new ThreadStart( ThreadProc ) ).Start();

When I run this program, the result is astonishing. The output in console
is: 'ThreadState = Unknown.'
And the call to Plantwrap failed with error:  -2147221008, 'CoInitialize has
not been called.'.

Can anybody help me out?
Thanks,
           YeJianWei
jacky kwok - 21 Sep 2006 07:22 GMT
> 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.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.