My application talks to another piece of software (Catia V6/3DEXPERIENCE), using various .dll's.
Some of the functionality in this interface appears to run slightly asynchronously, which is causing occasional failures in my script.
"AnyObject" and "Selection" types, along with their functions, come from the .dll; the "IsolateThis" function is embedded in my own class.
public INFITF.AnyObject IsolateThis(INFITF.AnyObject thing)
{
Selection osel = catapp.ActiveEditor.Selection; //get a selection object
osel.Clear(); //make sure nothing pre-selected
osel.Add(thing); //add item to be copied to selection
osel.Copy(); //copy
osel.Clear(); //clear selection
osel.Add(this.hbod); //add item to paste into
osel.Paste(); //paste
this.part.Update(); //update the parent object
return (INFITF.AnyObject)osel.Item(1).Value; //return pasted result
}
The function basically copies one object, pastes it into another, and returns the resulting pasted object. The Selection object is kind enough to update itself to only contain the pasted thing after you paste, which is why the last line works.
For some reason it fine when I step through the code, but if I try to use the returned object elsewhere in real-time then I sometimes get an invalid cast exception, for example on the following line:
CATCurve tmpcurve = new CATCurve((HybridShapeCurveExplicit)tmpbody.IsolateThis(this.Branch.shape));
Which does not recur when I hit F10 to continue. My suspicion is that it's firing a paste command to Catia, which takes time to complete, but is ploughing on with the rest of my code without requiring a response. This results in the wrong object being returned (as 'this.hbod' is still seen as the selected object, which is not compatible with the expected return object).
How can I wrap/refactor this to ensure that the paste and update operations have fully completed? Is this even possible with things drawn from external references?
I'm still pretty new to c#. I've tried looking into using Task and Process but to be honest, am quite lost as to how to implement them in this situation.