1

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?

1

Did you follow this set of best practices for multithreaded arcobjects code? Objects cannot cross thread boundaries and it appears that your code is doing so. Figure out the data source on the main UI thread and serialize the data source object to XML as recommended there.

| improve this answer | |
  • I followed it, I reopened all feature classes by its string names, I created the new feature class in a fresh In-Memory-Workspace Owned by the worker thread, even thought, it continues crashing. – Angie Feb 13 '15 at 23:11
  • You may need to disconnect the layer on the main thread then? Maybe there's a schema lock. What's the debugger telling you? – Jason Scheirer Feb 13 '15 at 23:12
  • When ArcMap crashes the debugger session ends without any information. – Angie Feb 13 '15 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.