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...]
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.
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