Geoprocessor in separate thread fails the second time it's run

Discussion created by edienske on May 15, 2013
Latest reply on May 21, 2013 by afmaps
Hi all,

I'm developing a lengthy process where I'm running three separate geoprocessing tools from an ArcMap (10.0) command inside a separate STA thread. I'm doing it this way so I can have my process return feedback messages to my UI (probably other ways of doing this but I have my reasons, so bear with me).

For each geoprocessing tool I'm defining a new ESRI.ArcGIS.Geoprocessor.Geoprocessor, which then runs the tool (SpatialJoin, then Select, then Intersect):

            SpatialJoin spatialJoin = new SpatialJoin();
            [define spatialJoin parameters...]

            Geoprocessor geoprocessor = new Geoprocessor();
            geoprocessor.AddOutputsToMap = false;
            geoprocessor.OverwriteOutput = true;
            geoprocessor.Execute(spatialJoin, null);

The process runs without error or issue inside the new thread - but only the first time. The thread ends properly once it's finished running. The trouble starts when I'm running the same process a second time (without restarting ArcMap). It starts the separate thread again, but then fails executing the first geoprocessing tool, stating "COM object that has been separated from its underlying RCW cannot be used."

When I list the messages inside the Geoprocessor, it lists the messages from the last tool it ran (i.e. the Intersect from the first time)! It seems that somewhere it keeps a reference to some kind of internal geoprocessing object.

I should add that the process running inside the new thread does not use any objects from the main thread (including database connections) and is therefore completely self-contained.

I have tried adding this and it did not help:


This didn't help either:

            IGeoProcessor processor = geoprocessor.IGeoProcessor;

I thought this might help, but I'm not running ArcEngine:


So the question is: how can this be solved....?
Thanks in advance,