Refer to COM object in the worker thread in C#

-2

I have a COM object SerInterface objCANinterface; which is created in VC++ 6.0 ATL which i was able to successfully call the methods from my C# GUI.(ThreadingModel is Both when checked in registry)

But i have created a thread which has to use this object opened inside this thread to continues the process.

so i have added the following statements in my constructor class as

public MyClass()
        {
 // Start the thread with a ParameterizedThreadStart.
            ParameterizedThreadStart start = new ParameterizedThreadStart(callBackLoad);
            threadFlash = new Thread(start);
            threadFlash.SetApartmentState(ApartmentState.STA);
...
}

void myFun()
{
    threadFlash.Start(objCANinterface);
   threadFlash.Join(); //Wai
}

[STAThread]
public void callBackLoad(object refinterface)
{
//I am not able to access objCANinterface inside my thread so i have passed the object as a parameter and trying to cast 
 SerInterface objSrinterface = (SerInterface)refinterface;
}

This is not successful Could you please let us know how do i solve this issue. I am getting the following error

An unhandled exception of type 'System.InvalidCastException' occurred in MyClass.exe

Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'MYINTERFACELib.SerInterface'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{37C34F3C-0082-46F5-9974-37CEB2E1C2EE}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

Please let me know how to solve this issue. Thanks in advance.

@ oberfreak......... have you tried it by using "as" as well? Wen you step down with a debugger, which type is it, is it the expected type? Are your interops included in the assembly or in a seperate file?

@ oberfreak, how do i use "As", could you please let me know how to use that. My interops are included along with my exe. I was able to perfectly work out of the thread.

@ Hans Passant: You forgot to register the type library in your .rgs script and you also didn't create the proxy/stub project. A threading model of Both still requires an object to be marshaled when it was created on an STA thread and used on another STA thread. Which tends to make it pointless to create a new thread. Get ahead by creating the object on the worker thread instead.

@ Hans Passant : I have registered my type library and interop also created in the C# which is making me to use inside my C# code.I cannot create a new object as I need to use the same object to continue the CAN sequence commands. Could you please provide any sample for better marshalling inside a thread.

c#
multithreading
object
com
call
asked on Stack Overflow Oct 11, 2012 by user1315385 • edited Oct 12, 2012 by user1315385

1 Answer

0

I have unregistered/removed the reference from the Exe and have registered and added the reference back to the tool. Then it worked with the parameter thread passing which made as STA thread. Thanks for the answers proposed.

answered on Stack Overflow Oct 14, 2012 by user1315385

User contributions licensed under CC BY-SA 3.0