2

I use BackgroundWorker to create asynchronous class elements. When running to

IFeatureClass pFeatureClass = pFeatureWorksapce.CreateFeatureClass(pFileName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");

the form of AppCrash is popped up. The error code is c0000005. When I no longer use BackgroundWorker to create a FeatureClass. This method is normal.

mBackgroundWorker = new BackgroundWorker();
mBackgroundWorker.DoWork += new DoWorkEventHandler(mBackgroundWorker_DoWork);

private void mBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    FeatureClassCreatorParams pParams = new FeatureClassCreatorParams();
    IFeatureClass pFeatureClass = CreateShapefile(pParams);
}
public IFeatureClass CreateShapefile(FeatureClassCreatorParams param)
{
    if (param == null)
    {
        throw new ArgumentNullException("FeatureClassCreatorParams is null");
    }
    param.Verify();
    List<FieldClassInfo> pFieldList = ReadXml(param.XmlPath);
    IFeatureClass pFeatureClass = CreateShpFileStructure(param.SavePath, pFieldList, param.GeometryType, param.SpatialReference, param.HasZ);
    return pFeatureClass;
}
private IFeatureClass CreateShpFileStructure(string fileName, List<FieldClassInfo> fields, esriGeometryType geoType, ISpatialReference spatialReference, bool hasZ)
{
    string pFloder = System.IO.Path.GetDirectoryName(fileName);
    string pFileName = System.IO.Path.GetFileNameWithoutExtension(fileName);
    if (!Directory.Exists(pFloder))
    {
        Directory.CreateDirectory(pFloder);
    }
    IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
    IFeatureWorkspace pFeatureWorksapce = pWorkspaceFactory.OpenFromFile(pFloder, 0) as IFeatureWorkspace;
    IFields pFields = new FieldsClass();
    IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

        // 添加几何字段
    IField pShapeField = new FieldClass();
    IFieldEdit pShapeFieldEdit = pShapeField as IFieldEdit;
    pShapeFieldEdit.Name_2 = "Shape";
    pShapeFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

    IGeometryDef pGeometryDef = new GeometryDefClass();
    IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
    pGeometryDefEdit.GeometryType_2 = geoType;
    pGeometryDefEdit.HasZ_2 = hasZ;
    pGeometryDefEdit.SpatialReference_2 = spatialReference;
    pShapeFieldEdit.GeometryDef_2 = pGeometryDef;
    pFieldsEdit.AddField(pShapeField);

        //foreach (FieldClassInfo field in fields)
        //{
        //    IField pField = new FieldClass();
        //    IFieldEdit pFieldEdit = pField as IFieldEdit;
        //    pFieldEdit.Name_2 = field.Name;
        //    pFieldEdit.Type_2 = field.FieldType;
        //    pFieldEdit.AliasName_2 = field.Caption;
        //    pFieldEdit.Length_2 = field.Length;
        //    pFieldsEdit.AddField(pField);
        //}

    // Throw Error:appCrash
    IFeatureClass pFeatureClass = pFeatureWorksapce.CreateFeatureClass(pFileName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
    }
6
  • 2
    Did you try using Activator.CreateInstance for your workspacefactory? resources.esri.com/help/9.3/ArcGISengine/dotnet/… Commented Oct 12, 2018 at 14:31
  • 1
    What is the result if, instead of making a BackgroundWorker, you just created and started a new Thread? Be sure to set the ApartmentState to STA.
    – danielm
    Commented Oct 12, 2018 at 19:36
  • I want to create a shapefile with BackgroundWorker
    – Feng Ma
    Commented Oct 15, 2018 at 1:22
  • 1
    If you are using ArcGIS desktop objects then yes, your problem is because of the background worker. ArcGIS is a single thread application, the objects are not thread safe. This does not mean you can't use multiple threads just that you can't pass objects over the thread boundaries... instead of BackgroundWorker use system.threading.thread with ApartmentState of STA, create a new instance as @KirkKuykendall indicates then only pass non-Esri objects over the threads; there are some exceptions to this rule, but that's another topic in itself born of a thousand facepalm moments! Commented Oct 19, 2018 at 0:10
  • 1
    Search for "threads in isolation" for more info. Commented Oct 19, 2018 at 16:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.