我需要一个C#循环来重复调用COM对象上的函数 . 单线程可以工作,但并行版本会抛出......

foreach (Stub stub in inputStubs) { dc.Work(stub); }   // ok

Parallel.ForEach(inputStubs, stub => dc.Work(stub) ); // throws

在这两种情况下,我都可以在Work()中找到断点,以便调用它 . 当Work()中的所有功能都被注释掉时,仍会发生异常 . 例外情况如下:

Exception thrown: 'System.InvalidCastException' in my.dll

Additional information: Unable to cast COM object of type 'System.__ComObject'
to interface type 'my.IDC'. This operation failed because the QueryInterface
call on the COM component for the interface with IID
'{99E2873B-4042-4F24-A680-2882E4C869DD}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

COM对象的C#定义就像这样开始 .

[ComImport, Guid("99E2873B-4042-4F24-A680-2882E4C869DD"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
SuppressUnmanagedCodeSecurity()]
public interface IDC
{

在C方面,样板材料归结为

interface __declspec(uuid("99E2873B-4042-4F24-A680-2882E4C869DD"))
IDC : IUnknown
{

class CDC : public IDC
{
     private:
         LONG refcount;

     public:

     CDC() { refcount = 1; }
     virtual ~CDC() {}

     virtual HRESULT __stdcall QueryInterface(REFIID riid, void** p)
     {
         if(riid == __uuidof(IUnknown)) { *p = static_cast<IUnknown*>(this); AddRef(); return 0; }
         if(riid == __uuidof(IDC))          { *p = static_cast<T*>(this);   AddRef(); return 0; }
         return E_NOINTERFACE;
     }

     virtual ULONG __stdcall AddRef() 
     {
         return InterlockedIncrement(&refcount);
     }

     virtual ULONG __stdcall Release() 
     { 
         ULONG count = InterlockedDecrement(&refcount);
         if(!count) 
            delete this;  
         return count;
     }

可能是什么导致了这个?