Standalone program crashes after using Geoprocessor

1951
5
10-25-2012 03:53 PM
ChrisLesher
New Contributor
I'm writing a standalone application that executes the "Create feature class" tool using Geoprocessor.  The tool runs successfully.  However when the program exits is when I have a problem.  The program either hangs indefinitely or crashes upon exiting the Main method.  This is in C# using ArcGIS 10.1 against a geodatabase on SQL Server 2012.

I've read about the necessity of manually freeing COM objects sometimes in order to avoid these problems.  But all the sample code I've ever seen that uses Geoprocessor doesn't do this.  Various samples show the Geoprocessor object and whatever tool object is being used with it created normally using "new", with no special handling done to free them afterwards.

As a test I also tried running the "Copy" tool and had the same problem.  As another test I ran the same code against a file geodatabase and did not run into this issue.  It's against a geodatabase in SQL server that it happens.  I tried both an enterprise geodatabase and a SQL Express geodatabase with the same result.

I've pared my code down to the small example below that demonstrates the problem.  Again the tool runs successfully and the new feature class is created from the specified template.  No problem.  It's upon exit from Main that I get a crash.

Any ideas on how to just use Geoprocessor from C# successfully?


using System;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.DataManagementTools;

namespace TestGP
{
    class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new TestGP.LicenseInitializer();
    
        [STAThread()]
        static void Main(string[] args)
        {
            m_AOLicenseInitializer.InitializeApplication(
                new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced },
                new esriLicenseExtensionCode[] { });

            string workspacePathname = @"C:\Users\username\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\connection.sde";

            Geoprocessor gp = new Geoprocessor();
            gp.SetEnvironmentValue("workspace", workspacePathname);

            CreateFeatureclass tool = new CreateFeatureclass();
            tool.out_path = workspacePathname;
            tool.out_name = "MyNewRoads";
            tool.template = "Geo1.DBO.RoadTemplate";
            tool.has_m = "SAME_AS_TEMPLATE";
            tool.has_z = "SAME_AS_TEMPLATE";

            object sev = null;
            try
            {
                gp.Execute(tool, null);
            }
            catch (Exception exp)
            {
                Console.WriteLine("Exception:  " + exp.Message);
                Console.WriteLine(exp.StackTrace);
            }
            finally
            {
                Console.WriteLine(gp.GetMessages(ref sev));
            }
            Console.WriteLine("Created new feature class");
        }
    }
}
0 Kudos
5 Replies
ChrisLesher
New Contributor
I still have no idea why this program crashes or hangs after using the GeoProcessor.  But I found it's easy to create a new feature class based on a template using code like this:

        public static IFeatureClass CreateFeatureClassByTemplate(
            IWorkspace workspace,
            string newFeatureClassName,
            IFeatureClass template,
            string configKeyword = null)
        {
            IFeatureWorkspace fWorkspace = (IFeatureWorkspace)workspace;
            return fWorkspace.CreateFeatureClass(newFeatureClassName,
                (IFields)((IClone)template.Fields).Clone(),
                template.CLSID,
                template.EXTCLSID,
                template.FeatureType,
                template.ShapeFieldName,
                configKeyword);
        }
0 Kudos
MeleKoneya
Occasional Contributor III
I am having the same problem with Code I migrated from 10.0 to 10.1.    I have the exact same references in a VS 2010 project as you but I am Deleting and Appending Features.  My code works in 10.0, but after migrating it to 10.1, the console application either crashes or hangs up after executing the code much like you are seeing.

I currently have a support ticket in and it is starting to sound like a bug with 10.1
0 Kudos
MeleKoneya
Occasional Contributor III
This is a bug with the Geoprocessor.Excecute method.

NIM090973
0 Kudos
JasonPike
Occasional Contributor
I'm writing a standalone application that executes the "Create feature class" tool using Geoprocessor.  The tool runs successfully.  However when the program exits is when I have a problem.  The program either hangs indefinitely or crashes upon exiting the Main method.  This is in C# using ArcGIS 10.1 against a geodatabase on SQL Server 2012.

I've read about the necessity of manually freeing COM objects sometimes in order to avoid these problems.  But all the sample code I've ever seen that uses Geoprocessor doesn't do this.  Various samples show the Geoprocessor object and whatever tool object is being used with it created normally using "new", with no special handling done to free them afterwards.

As a test I also tried running the "Copy" tool and had the same problem.  As another test I ran the same code against a file geodatabase and did not run into this issue.  It's against a geodatabase in SQL server that it happens.  I tried both an enterprise geodatabase and a SQL Express geodatabase with the same result.

I've pared my code down to the small example below that demonstrates the problem.  Again the tool runs successfully and the new feature class is created from the specified template.  No problem.  It's upon exit from Main that I get a crash.

Any ideas on how to just use Geoprocessor from C# successfully?


using System;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.DataManagementTools;

namespace TestGP
{
    class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new TestGP.LicenseInitializer();
    
        [STAThread()]
        static void Main(string[] args)
        {
            m_AOLicenseInitializer.InitializeApplication(
                new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced },
                new esriLicenseExtensionCode[] { });

            string workspacePathname = @"C:\Users\username\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\connection.sde";

            Geoprocessor gp = new Geoprocessor();
            gp.SetEnvironmentValue("workspace", workspacePathname);

            CreateFeatureclass tool = new CreateFeatureclass();
            tool.out_path = workspacePathname;
            tool.out_name = "MyNewRoads";
            tool.template = "Geo1.DBO.RoadTemplate";
            tool.has_m = "SAME_AS_TEMPLATE";
            tool.has_z = "SAME_AS_TEMPLATE";

            object sev = null;
            try
            {
                gp.Execute(tool, null);
            }
            catch (Exception exp)
            {
                Console.WriteLine("Exception:  " + exp.Message);
                Console.WriteLine(exp.StackTrace);
            }
            finally
            {
                Console.WriteLine(gp.GetMessages(ref sev));
            }
            Console.WriteLine("Created new feature class");
        }
    }
}


Can you attach a solution that compiles and executes? I would like to take a look at this.
0 Kudos
MeleKoneya
Occasional Contributor III
I was able to work around this bug by putting the following as the last line of code in my application:

Process.GetCurrentProcess().Kill()
0 Kudos