Quantcast
Channel: Question and Answer » multi-threading
Viewing all articles
Browse latest Browse all 3

Problem with multi-threading and geoprocessor tool in c#

$
0
0

What I am doing: I am trying to run a geoprocessor tool in a background thread other than UI thread. I am passing string path of arcObjects and creating that arcObject in my background thread. Then I am passing those arcObjects to geoProcessor tool.

In UI thread, Button’s on click method:

private void btnFindInterction_Click(object sender, EventArgs e)
{
    List<IFeatureLayer> layers = GetAllMapFeatureLayers();

    string featureLayerPath1 = GetFeatureLayerPath(layers[0]);
    string featureLayerPath2 = GetFeatureLayerPath(layers[1]);

    Thread worker = new Thread(new ThreadStart(() =>
        {
            IFeatureClass featureClass1 = GetFeatureClassFromPath(featureLayerPath1);
            IFeatureClass featureClass2 = GetFeatureClassFromPath(featureLayerPath2);

            var result = RunIntersectTool(featureClass1, featureClass2);
            OnCalculationComplete(intersectedFeatureClasssPath);
        }
    ));

    worker.SetApartmentState(ApartmentState.STA);
    worker.Start();
}

The RunIntersectTool method:

string RunIntersectTool(IFeatureClass featureClass1, IFeatureClass featureClass2)
{
    IWorkspace workspace = GetTempGDBWorkspace();
    string intersectFeatureClassName = "Intersect_result";
    string intersectFeatureClassPath = workspace.PathName + "\" + intersectFeatureClassName;
    try
    {
        var gp = new Geoprocessor { OverwriteOutput = true, AddOutputsToMap = false };
        IGpValueTableObject gpValueTableObject = new GpValueTableObjectClass();
        gpValueTableObject.SetColumns(1);
        var inFeature1 = imperviousFeatureClass as object;
        gpValueTableObject.AddRow(inFeature1);
        var inFeature2 = watershedFeatureClass as object;
        gpValueTableObject.AddRow(inFeature2);

        var intersectTool = new Intersect
        {
            in_features = gpValueTableObject,
            out_feature_class = intersectFeatureClassPath 
        };
        gp.Execute(intersectTool, null);
        return intersectFeatureClassPath;
    }
    catch (Exception e)
    {
        return null;
    }
}

What problem am I facing:
The tool runs perfectly in some machines, but gives error in some machine: “000732 : : Dataset does not exist or is not supported” And this error never happen if I don’t use threading. I read help files of Running a geoprocessing tool using background geoprocessing, but I couldn’t find anything that says I can’t pass arcObjects.

There is another problem I am facing. Whenever I ran the tool, it works fine first time(in some machines), but gives error when it creates an instance of geoprocessor at second time. Error message: “Attempted to read or write protected memory”. I guess this error is also related to multithreading and arcObjects.


Viewing all articles
Browse latest Browse all 3

Trending Articles