Sunday, 19 August 2018

c# - Schema Lock issue when using ITable.AddField via ArcObjects


During a process I am running, I sometimes need to create a field that is missing in an attribute table. Unfortunately, the program is complaining that the table I am adding the field to has a schema lock from the program itself. So I attempted to add the ISchemaLock method for granting an exclusive lock and I now get the HRESULT error 0x8004022D when attempting to change the lock. Here is what the code looks like.



private void DoWork()
{
IWorkspace workspace = getWorkspacefromPath(workspacepath);
IFeatureWorkspace fWorkspace = (IFeatureWorkspace)workspace;
IFeatureClass fc = fWorkspace.OpenFeatureClass(targetfile);
if (fc == null)
{
MessageBox.Show("No feature class found.", "Error.");
return;
}

ITable table = (ITable)fc;
int index = table.FindField("PSTATE");
if (index == -1)
{
ISchemaLock schemaLock = (ISchemaLock)table;
settesttext("No PSTATE field found. Adding field now.");
try
{

schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);

table.AddField(fielddata());

}
catch (Exception error)
{
MessageBox.Show("Field creation error. Closing process." + "\n " + error.Message);
return;

}
finally

{
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}
settesttext("Field creation complete");
index = table.FindField("PSTATE");
}

How should I go about adding the field to avoid getting a schema lock problem?



Answer



It appears that the lock was due to the IFeatureWorkspace maintaining a reference to the file GDB I was using.



Adding...


        fWorkspace = null;
GC.Collect();

after creating the IFeatureClass instance did the trick. Thanks @blah238 for making me consider the workspace.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...