Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 是否可以在C中调用将this.Handle作为后台工作程序内的参数的方法#_Multithreading_C# 4.0_Solidworks - Fatal编程技术网

Multithreading 是否可以在C中调用将this.Handle作为后台工作程序内的参数的方法#

Multithreading 是否可以在C中调用将this.Handle作为后台工作程序内的参数的方法#,multithreading,c#-4.0,solidworks,Multithreading,C# 4.0,Solidworks,我正在用C语言编写一个独立的WinForm程序,它使用Solidworks EPDM api。该程序获取顶级部件,并查找部件中的所有引用和引用文件。e、 g.所有子组件、零件文件和图纸。然后程序从EPDM检出所有文件,更新数据卡,并将所有文件检入EPDM 我已经成功地实现了代码中查找所有引用和引用文件的部分,并使用后台工作人员更新了数据卡信息。这部分代码不需要访问UI线程。我想能够添加代码,签出文件,并在后台工作人员中将其签回。问题是用于签出和签入的方法将this.Handle作为参数。我知道从

我正在用C语言编写一个独立的WinForm程序,它使用Solidworks EPDM api。该程序获取顶级部件,并查找部件中的所有引用和引用文件。e、 g.所有子组件、零件文件和图纸。然后程序从EPDM检出所有文件,更新数据卡,并将所有文件检入EPDM

我已经成功地实现了代码中查找所有引用和引用文件的部分,并使用后台工作人员更新了数据卡信息。这部分代码不需要访问UI线程。我想能够添加代码,签出文件,并在后台工作人员中将其签回。问题是用于签出和签入的方法将this.Handle作为参数。我知道从后台工作程序中访问UI线程会引发跨线程异常。代码不访问任何UI控件。它只需要访问此.Handle。是否可以以线程安全的方式将此.Handle传递给后台工作程序,而不会引发跨线程异常

这是我第一次使用背景工作者,所以我的知识有限。下面是我希望在后台工作程序中运行的代码

    private void BatchCheckout(Dictionary<string, string> SelectedFiles)
    {
        try
        {
            IEdmBatchGet batchGetter = (IEdmBatchGet)vault.CreateUtility(EdmUtility.EdmUtil_BatchGet);           
            EdmSelItem[] ppoSelection = new EdmSelItem[SelectedFiles.Count];            
            IEdmFile5 aFile;
            IEdmFolder5 aFolder;
            IEdmFolder5 ppoRetParentFolder;
            IEdmPos5 aPos;
            int i = 0;

            foreach (KeyValuePair<string, string> kvp in SelectedFiles)
            {
                aFile = vault1.GetFileFromPath(kvp.Key, out ppoRetParentFolder);
                aPos = aFile.GetFirstFolderPosition();
                aFolder = aFile.GetNextFolder(aPos);
                ppoSelection[i] = new EdmSelItem();
                ppoSelection[i].mlDocID = aFile.ID;
                ppoSelection[i].mlProjID = aFolder.ID;
                i = i + 1;
            }
            batchGetter.AddSelection((EdmVault5)vault1, ref ppoSelection);
            batchGetter.CreateTree(this.Handle.ToInt32(), (int)EdmGetCmdFlags.Egcf_Lock);
            batchGetter.GetFiles(this.Handle.ToInt32(), null);
        }

        catch (System.Runtime.InteropServices.COMException ex)
        {
            MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\n" + GetStackTrace(ex));
        }
    }
private void批处理签出(字典选择的文件)
{
尝试
{
IEdmBatchGet batchGetter=(IEdmBatchGet)vault.CreateUtility(EdmUtility.EdmUtil\u BatchGet);
EdmSelItem[]ppoSelection=新建EdmSelItem[SelectedFiles.Count];
IEdmFile5文件;
IEdmFolder5 a文件夹;
IEdmFolder5 ppoRetParentFolder;
IEdmPos5载脂蛋白;
int i=0;
foreach(所选文件中的KeyValuePair kvp)
{
aFile=vault1.GetFileFromPath(kvp.Key,out-ppoRetParentFolder);
aPos=aFile.GetFirstFolderPosition();
aFolder=aFile.GetNextFolder(aPos);
[i]=新项目();
[i].mlDocID=aFile.ID;
[i].mlProjID=aFolder.ID;
i=i+1;
}
batchGetter.AddSelection((EdmVault5)Vault 1,参考1);
CreateTree(this.Handle.ToInt32(),(int)EdmGetCmdFlags.Egcf_Lock);
batchGetter.GetFiles(this.Handle.ToInt32(),null);
}
捕获(System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show(“HRESULT=0x”+ex.ErrorCode.ToString(“X”)+“”+ex.Message);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message+“\n”+GetStackTrace(例如));
}
}
多年来,我一直是StackOverflow的读者,几乎所有我遇到的问题都找到了答案。这是我第一个关于StackOverflow的问题。我真的希望有人能回答这个问题

编辑:

我已经成功地测试了AndrewK的建议,并且很高兴地报告它确实对我的批量检查方法有效。当我在后台工作程序中运行批处理签入方法时,会出现以下COM异常:

无法将“System.\u ComObject”类型的COM对象强制转换为接口类型“EPDM.Interop.EPDM.IEdmBatchUnlock2”。此操作失败,因为对IID为“{F0970446-4CBB-4F0F-BAF5-F9CD2E09A5B3}”的接口的COM组件的QueryInterface调用由于以下错误而失败:不支持此类接口(HRESULT的异常:0x80004002(E_NOINTERFACE))

只有从后台工作程序运行代码时,才会出现此异常

以下是我的BatchCheckin方法中的代码:

    private void BatchCheckin(Dictionary<string, string> SelectedFiles)
    {
        try
        {
            int i = 0;
            IEdmFolder5 ppoRetParentFolder;
            IEdmFile5 aFile;
            IEdmFolder5 aFolder;
            IEdmPos5 aPos;
            EdmSelItem[] ppoSelection = new EdmSelItem[SelectedFiles.Count];
            IEdmBatchUnlock2 batchUnlock;

            foreach (KeyValuePair<string, string> kvp in SelectedFiles)
            {
                aFile = vault5.GetFileFromPath(kvp.Key, out ppoRetParentFolder);
                aPos = aFile.GetFirstFolderPosition();
                aFolder = aFile.GetNextFolder(aPos);
                ppoSelection[i] = new EdmSelItem();
                ppoSelection[i].mlDocID = aFile.ID;
                ppoSelection[i].mlProjID = aFolder.ID;
                i = i + 1;
            }

            batchUnlock = (IEdmBatchUnlock2)vault7.CreateUtility(EdmUtility.EdmUtil_BatchUnlock);
            batchUnlock.AddSelection((EdmVault5)vault5, ref ppoSelection);
            batchUnlock.CreateTree(0, (int)EdmUnlockBuildTreeFlags.Eubtf_ShowCloseAfterCheckinOption + (int)EdmUnlockBuildTreeFlags.Eubtf_MayUnlock);
            batchUnlock.Comment = "Updates";
            batchUnlock.UnlockFiles(0, null);
        }

        catch (System.Runtime.InteropServices.COMException ex)
        {
            MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\n" + GetStackTrace(ex));
        }
    }
private void BatchCheckin(字典选择的文件)
{
尝试
{
int i=0;
IEdmFolder5 ppoRetParentFolder;
IEdmFile5文件;
IEdmFolder5 a文件夹;
IEdmPos5载脂蛋白;
EdmSelItem[]ppoSelection=新建EdmSelItem[SelectedFiles.Count];
IEdmBatchUnlock2 batchUnlock;
foreach(所选文件中的KeyValuePair kvp)
{
aFile=vault5.GetFileFromPath(kvp.Key,out-ppoRetParentFolder);
aPos=aFile.GetFirstFolderPosition();
aFolder=aFile.GetNextFolder(aPos);
[i]=新项目();
[i].mlDocID=aFile.ID;
[i].mlProjID=aFolder.ID;
i=i+1;
}
batchUnlock=(IEdmBatchUnlock2)Vault 7.CreateUtility(EdmUtility.EdmUtil\u batchUnlock);
batchUnlock.AddSelection((EdmVault5)Vault 5,参考文件);
batchUnlock.CreateTree(0,(int)EdmUnlockBuildTreeFlags.Eubtf_ShowCloseAfterCheckInAction+(int)EdmUnlockBuildTreeFlags.Eubtf_MayUnlock);
batchUnlock.Comment=“更新”;
batchUnlock.UnlockFiles(0,空);
}
捕获(System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show(“HRESULT=0x”+ex.ErrorCode.ToString(“X”)+“”+ex.Message);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message+“\n”+GetStackTrace(例如));
}
}
我在调用Vault 7.CreateUtility时遇到异常。BatchCheckin代码与BatchCheckout几乎相同。我在两种方法中对Vault 7.CreateUtility进行相同的调用。唯一的区别是在BatchCheckin方法中将EdmUtility标志设置为EdmUtil_BatchUnlock。关于这件事有什么线索吗AndrewK

更新:

通过将batchUpdate从IEdmBatchUnlock2接口更改为IEdmBatchUnlock接口,我能够解决COM异常。以下是代码更改:

    private void BatchCheckin(Dictionary<string, string> SelectedFiles)
    {
        int i = 0;
        IEdmFolder5 ppoRetParentFolder;
        IEdmFile5 aFile;
        IEdmFolder5 aFolder;
        IEdmPos5 aPos;
        EdmSelItem[] ppoSelection = new EdmSelItem[SelectedFiles.Count];
        IEdmBatchUnlock batchUnlock = (IEdmBatchUnlock)vault7.CreateUtility(EdmUtility.EdmUtil_BatchUnlock);

        try
        {
            foreach (KeyValuePair<string, string> kvp in SelectedFiles)
            {
                aFile = vault5.GetFileFromPath(kvp.Key, out ppoRetParentFolder);
                aPos = aFile.GetFirstFolderPosition();
                aFolder = aFile.GetNextFolder(aPos);
                ppoSelection[i] = new EdmSelItem();
                ppoSelection[i].mlDocID = aFile.ID;
                ppoSelection[i].mlProjID = aFolder.ID;
                i = i + 1;
            }               
            batchUnlock.AddSelection((EdmVault5)vault5, ref ppoSelection);
            batchUnlock.CreateTree(0, (int)EdmUnlockBuildTreeFlags.Eubtf_ShowCloseAfterCheckinOption + (int)EdmUnlockBuildTreeFlags.Eubtf_MayUnlock);
            batchUnlock.Comment = "Release to Production ECO";
            batchUnlock.UnlockFiles(0, null);
        }

        catch (System.Runtime.InteropServices.COMException ex)
        {
            MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\n" + GetStackTrace(ex));
        }
    }
private void BatchCheckin(字典选择
        batchGetter.AddSelection((EdmVault5)vault1, ref ppoSelection);
        batchGetter.CreateTree(0, (int)EdmGetCmdFlags.Egcf_Lock);
        batchGetter.GetFiles(0, null);