Terminating a GP tool while running (in VB.Net)

08-09-2013 06:29 AM
New Contributor II
I have a UI that has a button that exports currently selected vector features to a KML using the LayerToKML Geoprocessor.  Whilst it works great on locally stored featureclasses there are certain featureclasses (eg statewide cadastre) on our greater network that are way too big (and our WAN network is very slow!!), and hence take forever to process, even if just a few features are selected for export.   While part of my code does a quick check to see how many features exist in the dataset before executing the GP, and gives a warning that it may take until the end of time itself to complete, I want to allow the user to terminate the LayerToKML process should they find them selves onto their second cup of coffee while it is running.  Currently if my form is closed whilst this GP is running the whole ArcGIS drops out and all is lost.

Is this possible to do programatically?

This is my GP sub

Private Sub KMZGenerator(pfeaturelayer As IFeatureLayer, LayerName As String, FolderPath As String)

        ' Initialize the geoprocessor.
        Dim GP As Geoprocessor = New Geoprocessor()
        'Calculates current date and time and provides as a string
        Dim rightNow As DateTime = DateTime.Now
        Dim strCurrentDateTimeString As String
        strCurrentDateTimeString = rightNow.ToString("ddd_dd_MMM_yyyy_hh_mm_sstt")

        'Creates KMZ
        Dim LayerToKMLTool As ESRI.ArcGIS.ConversionTools.LayerToKML = New ESRI.ArcGIS.ConversionTools.LayerToKML()
        GP.SetEnvironmentValue("workspace", FolderPath)
        LayerToKMLTool.layer = pfeaturelayer
        LayerToKMLTool.layer_output_scale = 1
        LayerToKMLTool.out_kmz_file = LayerName & "_" & strCurrentDateTimeString & ".kmz"

        GP.Execute(LayerToKMLTool, Nothing)

    End Sub

Thnx Justin
0 Kudos
2 Replies
Regular Contributor II
I have never actually done it so I don't know how well it works but the second argument you pass to the execute function is an ITrackCancel.  If you implement the GP track cancel and pass it in, you should be able to let the user cancel the operation.
0 Kudos
New Contributor II
I never seemed to get that working.  Given my issue I decided to utilise gp.ExecuteAsync and Queue the geoprocessing tasks in the background.  Then if the GP tool took too long or hung the user could still save their project and cancel the GP task.
0 Kudos