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 2003

Tip: Looking for answers? Try searching our database.

dotnet interop run in a thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ahzeng - 18 Sep 2003 10:27 GMT
Hi,

I have a COM object implemented in ATL. The CoClass implements two
interface. The default interface IInterface1 is a dispatch interface. The
non-default interface IInterface2 is an IUnknown interface.

Using dotnet interop I can create object instances, I can also be able to
cast the instance to IInterface2. dotnet interop sounds perfect.

However when I wrap this small piece of code in a separate thread, I get a
cast invalid exception. Let me descibe it in more detail using pseudo-code.

public static void TestInterop()
{
   MyCom c = new MyCom();
   IInterface1 i1 = (IInterface1)c;
   IInterface2 i2 = (IInterface2)c;
}

when I directly call this function from the main thread, everything works
perfectly. However when I call the this function using the following code:

public static void Main()
{
   Thread t = new Thread(new ThreadStart(TestInterop)));
   t.Start();
   t.Join();
}

I got the disgusting cast invalid exception. Although I run the code in a
thread, I create the object in the thread, I run the code in the same
thread. I never try to call the function from other threads. I just do not
understand it really matters to run it from the main thread or from a
separate thread.

To make sure it is not a problem from my COM component. I then program in
VC++. With CreateThread, it works OK. So anybody have the same experience?
Do I miss something here? Or it is interop bug?
Christian Fröschlin - 18 Sep 2003 10:43 GMT
> public static void Main()
> {
>     Thread t = new Thread(new ThreadStart(TestInterop)));
>     t.Start();
>     t.Join();
> }

Does it help to set t.ApartmentState to STA before calling t.Start() ?

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.