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", "");
}