I'm developing a ArcMap add-in that get dynamic feeds from the network and append the newly arrived data to a suitable featurelayer.
I'm using InMemoryWorkspace, FeatureBuffer, FeatureCursor, Thread
.
void DoJob()
{
Thread th = new Thread(run);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
private void Run()
{
IFeatureClass ipTargetFC = ...//get FeatureClass from the suitable FeatureLayer in TOC
IFeatureCursor featuresToCopy = ...//parse the incomming data and create `in memory workspace` and a new FeatureClass holding the parsed data
ArcMap.Editor.StartOperation();
InsertFeaturesUsingCursor(ipTargetFC, featuresToCopy);
ArcMap.Editor.StopOperation("End Editing");
}
public static void InsertFeaturesUsingCursor(IFeatureClass ipTargetFC, IFeatureCursor featuresToCopy)
{
IFeatureCursor ipInsCursor = null;
try
{
IFeatureBuffer ipFBuff = ipTargetFC.CreateFeatureBuffer();
ipInsCursor = ipTargetFC.Insert(true);
IFeature ipFeat;
IFields ipTargetFields;
IField ipTargetField;
int featureOID;
while ((ipFeat = featuresToCopy.NextFeature()) != null)
{
ipFBuff.Shape = ipFeat.ShapeCopy;
ipTargetFields = ipTargetFC.Fields as IFields;
for (int i = 0; i < ipTargetFields.FieldCount; i++)
{
....//code for validating and copying fields' values from `featuresToCopy` to `ipFBuff` (ipFBuff.set_Value(i, value);)
}
featureOID = (int)ipInsCursor.InsertFeature(ipFBuff);
}
ipInsCursor.Flush();
}
catch (Exception ex) { throw ex; }
finally
{
if (ipInsCursor != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(ipInsCursor);
ipInsCursor = null;
}
}
}
My Problem Is:
when the execution reatches the line
featureOID = (int)ipInsCursor.InsertFeature(ipFBuff);
the ArcMap itself crashes
where is my fault?