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

2301
1
05-15-2013 04:26 AM
EvaDienske
New Contributor II
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:

            geoprocessor.ResetEnvironments();

This didn't help either:

            IGeoProcessor processor = geoprocessor.IGeoProcessor;
            Marshal.ReleaseComObject(processor);

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

https://sites.google.com/site/tosapage/programming/esri#MultiThread


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

Regards,
Eva
0 Kudos
1 Reply
AlexandraFairbarns
New Contributor III
Hi Eva,
I have recently tried to implement multithreading and found it a complete nightmare. Arc was not built to work in a multi threaded environment! Someone prove me wrong, but I don�??t believe that arc allows multiple instantiations of the geoprocessor to run tasks in parallel. Rather you have to send tasks to the geoprocessor asynchronously using a thread safe method. The following article explains a little more:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//000100000100000000
0 Kudos