锁定数组Com异常
问题描述:
我在使用线程中的API方法中的数组时遇到了问题。
锁定阵列不起作用。锁定数组Com异常
Thread thrCreate = new Thread(createThread);
thrCreate.SetApartmentState(ApartmentState.STA);
thrCreate.Start();
private void createThread()
{
IEdmAddCustomRefs2 pdmRefs = (IEdmAddCustomRefs2)vault2.CreateUtility(EdmUtility.EdmUtil_AddCustomRefs);
IEdmFile5 pdmRefFile = (IEdmFile5)pdmResult;
int[] iRefCount = new int[1];
iRefCount[0] = 1;
string[] strRefPath = new string[1];
strRefPath[0] = strPDFPath + strSerName + ".pdf";
lock (strRefPath)
{
lock (iRefCount)
{
pdmRefs.AddReferencesPath2(pdmRefFile.ID, ref strRefPath, ref iRefCount);
}
}
}
COM异常称为DISP_E_ARRAYISLOCKED。
答
我现在使用的AddReferences方法,我没有得到异常了。
下面是更新后的代码:
Thread thrCreate = new Thread(createThread);
thrCreate.SetApartmentState(ApartmentState.STA);
thrCreate.Start();
private void createThread()
{
IEdmAddCustomRefs2 pdmRefs = (IEdmAddCustomRefs2)vault2.CreateUtility(EdmUtility.EdmUtil_AddCustomRefs);
IEdmFile5 pdmRefFile = (IEdmFile5)pdmResult;
string[] strRefPath = new string[1];
strRefPath[0] = strPDFPath + strSerName + ".pdf";
pdmRefs.AddReferencesPath(pdmRefFile.ID, ref strRefPath);
}
一个'lock'声明不会在这里帮助。在HRESULT名称中引用的锁是包含在Variant中的COM数组的锁定。 – Richard
该错误基本上表明,在工作线程上运行此代码不是一个好主意。您必须向SolidWorks询问可能的情况,但预期的回应是“不这样做”。 –
在'IEdmAddCustomRefs2'的实现中,有一个安全的数组,其中调用['SafeArrayLock'](https://msdn.microsoft.com/en-us/library/windows/desktop/ms221492(v = vs.85) ).aspx)超过对['SafeArrayUnlock']的调用(https://msdn.microsoft.com/en-us/library/windows/desktop/ms221246(v = vs.85).aspx) – Richard