ArcObjects Library Reference  

GPEventListner

About the Geoprocessing events listener Sample

[C#]

GPEventListner.cs

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessor;
using GeoprocessorEventHelper;

namespace TestListner
{
  class GPEventListner
  {
    /// <summary>
    /// This sample console app demonstrates listening to GP events as they happen
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
        ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);

      GPMessageEventHandler gpEventHandler = new GPMessageEventHandler();

      //get an instance of the geoprocessor
      Geoprocessor GP = new Geoprocessor();
      //register the event helper in order to be able to listen to GP events
      GP.RegisterGeoProcessorEvents(gpEventHandler);

      //wire the GP events
      gpEventHandler.GPMessage += new MessageEventHandler(OnGPMessage);
      gpEventHandler.GPPreToolExecute += new PreToolExecuteEventHandler(OnGPPreToolExecute);
      gpEventHandler.GPToolboxChanged += new ToolboxChangedEventHandler(OnGPToolboxChanged);
      gpEventHandler.GPPostToolExecute += new PostToolExecuteEventHandler(OnGPPostToolExecute);

      //instruct the geoprocessing engine to overwrite existing datasets
      GP.OverwriteOutput = true;

      //create instance of the 'create random points' tool. Write the output to the machine's temp directory
      CreateFeatureclass createFeatureClass = new CreateFeatureclass(System.IO.Path.GetTempPath(), "RandomPoints.shp");
      //execute the tool
      GP.Execute(createFeatureClass, null);

      //unwire the GP events
      gpEventHandler.GPMessage -= new MessageEventHandler(OnGPMessage);
      gpEventHandler.GPPreToolExecute -= new PreToolExecuteEventHandler(OnGPPreToolExecute);
      gpEventHandler.GPToolboxChanged -= new ToolboxChangedEventHandler(OnGPToolboxChanged);
      gpEventHandler.GPPostToolExecute -= new PostToolExecuteEventHandler(OnGPPostToolExecute);

      //unregister the event helper
      GP.UnRegisterGeoProcessorEvents(gpEventHandler);

      System.Diagnostics.Trace.WriteLine("Done");
    }

    static void OnGPPostToolExecute(object sender, GPPostToolExecuteEventArgs e)
    {
      System.Diagnostics.Trace.WriteLine(e.Result.ToString());
    }

    static void OnGPToolboxChanged(object sender, EventArgs e)
    {
      System.Diagnostics.Trace.WriteLine("OnGPToolboxChanged");
    }

    static void OnGPPreToolExecute(object sender, GPPreToolExecuteEventArgs e)
    {
      System.Diagnostics.Trace.WriteLine(e.Description);
    }

    static void OnGPMessage(object sender, GPMessageEventArgs e)
    {
      System.Diagnostics.Trace.WriteLine(e.Message);
    }
  }
}

[Visual Basic .NET]

GPEventListner.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.DataManagementTools
Imports ESRI.ArcGIS.Geoprocessing
Imports GeoprocessorEventHelper

Namespace TestListner
  Friend Class GPEventListner
        ' This sample console app demonstrates listening to GP events as they happen

        Shared Sub Main(ByVal args As String())

            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)

            Dim gpEventHandler As GPMessageEventHandler = New GPMessageEventHandler()

            'get an instance of the geoprocessor
            Dim GP As IGeoProcessor2 = New ESRI.ArcGIS.Geoprocessing.GeoProcessor()
            'Dim GP As ESRI.ArcGIS.Geoprocessor.Geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor()
            'register the event helper in order to be able to listen to GP events
            GP.RegisterGeoProcessorEvents(gpEventHandler)

            'wire the GP events
            AddHandler gpEventHandler.GPMessage, AddressOf OnGPMessage
            AddHandler gpEventHandler.GPPreToolExecute, AddressOf OnGPPreToolExecute
            AddHandler gpEventHandler.GPToolboxChanged, AddressOf OnGPToolboxChanged
            AddHandler gpEventHandler.GPPostToolExecute, AddressOf OnGPPostToolExecute

            'instruct the geoprocessing engine to overwrite existing datasets
            GP.OverwriteOutput = True

            Dim parameters As IVariantArray = New VarArray()
            parameters.Add(System.IO.Path.GetTempPath())
            parameters.Add("NewShapefile.shp")

            ''create instance of the 'create random points' tool. Write the output to the machine's temp directory
            'Dim createFeatureclassTool As CreateFeatureclass = New CreateFeatureclass(System.IO.Path.GetTempPath(), "RandomPoints.shp")
            'execute the tool
            'GP.Execute(createFeatureclassTool, Nothing)
            GP.Execute("CreateFeatureclass_management", parameters, Nothing)

            'unwire the GP events
            RemoveHandler gpEventHandler.GPMessage, AddressOf OnGPMessage
            RemoveHandler gpEventHandler.GPPreToolExecute, AddressOf OnGPPreToolExecute
            RemoveHandler gpEventHandler.GPToolboxChanged, AddressOf OnGPToolboxChanged
            RemoveHandler gpEventHandler.GPPostToolExecute, AddressOf OnGPPostToolExecute

            'unregister the event helper
            GP.UnRegisterGeoProcessorEvents(gpEventHandler)

            System.Diagnostics.Trace.WriteLine("Done")
        End Sub

	Private Shared Sub OnGPPostToolExecute(ByVal sender As Object, ByVal e As GPPostToolExecuteEventArgs)
	  System.Diagnostics.Trace.WriteLine(e.Result.ToString())
	End Sub

	Private Shared Sub OnGPToolboxChanged(ByVal sender As Object, ByVal e As EventArgs)
	  System.Diagnostics.Trace.WriteLine("OnGPToolboxChanged")
	End Sub

	Private Shared Sub OnGPPreToolExecute(ByVal sender As Object, ByVal e As GPPreToolExecuteEventArgs)
	  System.Diagnostics.Trace.WriteLine(e.Description)
	End Sub

	Private Shared Sub OnGPMessage(ByVal sender As Object, ByVal e As GPMessageEventArgs)
	  System.Diagnostics.Trace.WriteLine(e.Message)
	End Sub
  End Class
End Namespace