Activator.CreateInstance locks in STAThread

A

ash.holland

Hi,
Does any one know why the creation of a COM object in .NET works when
it runs in the "Main" thread of a console application if the STAThread
tag is used, but same code causes the call to
Activator.CreateInstance(Type) locks up when called from another
thread, even though the thread has been set to STA?

Thanks in advance,
Ashley
 
W

Willy Denoyette [MVP]

Hi,
Does any one know why the creation of a COM object in .NET works when
it runs in the "Main" thread of a console application if the STAThread
tag is used, but same code causes the call to
Activator.CreateInstance(Type) locks up when called from another
thread, even though the thread has been set to STA?

Thanks in advance,
Ashley


What is the "ThreadingModel" of it's class as it is stored in the registry?

Willy.
 
A

ash.holland

What is the "ThreadingModel" of it's class as it is stored in the registry?

Hi Willy,

I did wonder about this as there is no String value defined for
ThreadingModel in the InprocServer32 KEY for the CLIS of the Object.I
do not have the source for the object, but I know that it is written
in Delphi and is an .ocx file type. Not sure what difference this
makes?I have tried setting the threading apartment to MTA and STA, but
both cause it to hang when calling Activator.CreateInstance. Tried
early binding with a new statement, but again this causes it to hang.

Cheers

Ashley
 
W

Willy Denoyette [MVP]

Hi Willy,

I did wonder about this as there is no String value defined for
ThreadingModel in the InprocServer32 KEY for the CLIS of the Object.I
do not have the source for the object, but I know that it is written
in Delphi and is an .ocx file type. Not sure what difference this
makes?I have tried setting the threading apartment to MTA and STA, but
both cause it to hang when calling Activator.CreateInstance. Tried
early binding with a new statement, but again this causes it to hang.

Cheers

Ashley

If there is no ThreadingModel specified, then it is considered "Single",
that means that the component is not designed to be accessed from multiple
threads, changing the model toe "Apartment" is in general not a good idea,
as the component was not really designed to live in an STA other than the
'main STA'. The "main STA" is the STA thread created by the COM
infrastructure, not your applications main thread.
Note that I say "Apartment", MTA or STA are NO legal values for
"ThreadingModel". I for one would never use such an object in today's code,
it was written for singled threaded applications, .NET applications are
multi-threaded by default.

Willy.
 

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