I am getting some weird errors while trying to implement multithreading with ArcObjects.
I am trying following things on per thread basis:
1) Open SDE Workspace
2) Open Feature class
3) Perform Icursor
Eventually, It fails with some errors like:
"Memory could not be read or write" or "AccessViolationException".
Looks like there are some memory leaks (though I don't know exactly where).
For more details please refer to the code below:
foreach (string featureClassName in settings.FeatureClassNames)
{
Thread thread = new Thread(new ParameterizedThreadStart(ReadFeatureClass));
thread.ApartmentState = ApartmentState.STA;
object[] parameters = CreateParameters(featureClassName);
thread.Start(parameters);
}
private void ReadFeatureClass(object parameters)
{
Settings settings;
string featureClassName;
ExtractParameters(parameters as object[], out settings, out featureClassName);
IFeatureWorkspace featureWorkspace = OpenFeatureWorkspace(settings);
ITable featureClass = featureWorkspace.OpenTable(featureClassName);
Marshal.ReleaseComObject(featureClass);
Marshal.ReleaseComObject(featureWorkspace);
}
private IFeatureWorkspace OpenFeatureWorkspace(Settings settings)
{
IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactory();
IPropertySet connectionProperties = new PropertySet();
connectionProperties.SetProperty("SERVER", settings.Server);
connectionProperties.SetProperty("INSTANCE", settings.Instance);
connectionProperties.SetProperty("USER", settings.User);
connectionProperties.SetProperty("PASSWORD", settings.Password);
connectionProperties.SetProperty("VERSION", settings.Version);
IFeatureWorkspace featureWorkspace = workspaceFactory.Open(connectionProperties, 0) as IFeatureWorkspace;
return featureWorkspace;
}