I have a method that does a bunch of geoprocessing on some layers for my ArcMap extension. It creates a feature-to-point layer for one FeatureClass and another feature-to-point layer for another FeatureClass. Then one of the feature-to-point layer's is used to as input to create a Buffer layer. This all works without any errors until I try to run the method in a separate thread. When I run it in a separate thread, both feature-to-point tools run without errors but it fails when it tries to create the buffer.
I borrowed error handling for my geoprocessing from Avoiding fails from ArcObjects geoprocessing with .NET?. The result shows the following.
Executing: Buffer GPL0 C:\Users\me\Documents\geoprocess.gdb\buffer "250 Feet" FULL ROUND NONE # esriGPMessageTypeProcessDefinition
Start Time: Tue Mar 06 18:55:10 2012 esriGPMessageTypeProcessStart
Failed to execute. Parameters are not valid. esriGPMessageTypeError
ERROR 000732: Input Features: Dataset GPL0 does not exist or is not supported
It seems when I try to run this in a separate thread, the name of the input FeatureClass to the buffer tool gets lost and changed to "GPL0" which doesn't exist. I've read that there can be issues when passing objects between threads but all the geoprocessing is happening in the same thread (as far as I understand). I pass an IMap variable and some other objects to the method. The IMap object seems to be passed okay since I can get its layers and the other objects are not used for the geoprocessing.
In my form:
backgroundWorker.DoWork += (obj, eArgs) => BgWorkerDoWork(map, ...)
bgwCalibrate.RunWorkerAsync();
private void BgWorkerDoWork(IMap map, ...)
{
var thread = new Thread(() => MyClass.MyGeoProcessMethod(map, ...));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
In my class MyClass:
private void MyGeoProcessMethod(IMap map, ...)
{
// do stuff...
// then create both feature-to-point files
IFeatureClass inFeatureClass1 = map.Layer[0] as IFeatureClass;
IFeatureClass inFeatureClass2 = map.Layer[1] as IFeatureClass;
// no errors:
IFeatureClass polygonsToPoints1 = CreateFeatureToPoint(inFeatureClass1);
IFeatureClass polygonsToPoints2 = CreateFeatureToPoint(inFeatureClass2);
// Create buffer (this is the part that fails)
IFeatureClass bufferFeatureClass = CreateBuffer(polygonsToPoints2);
}
Both feature-to-point layers get created successfully. The geoprocessing events say so and I can see the results on the map. Any suggestions or ideas on what's going on with the buffer part?