1

I got a strange issue in GP tool I don't really understand:

In my IGPFunction2.UpdateMessages() method, I use the following code to traverse all layers:

IEnumLayer enumLayer = gpUtils.GetMapLayers();
enumLayer.Reset();
ILayer layer = enumLayer.Next();
while (layer != null)
{
    <code here>
    layer = enumLayer.Next();
}

The purpose is to look for layers with duplicate names.

This works fine when I don't run the tool in the background. In background mode however, the Execute() method is never triggered.

This seems to happen after enumLayer.Next() is used. When removing the enumLayer.Next() and the code below, it works fine.

There are no error messages or exceptions occuring, and Arc claims that the tool executed succesfully. When calling IGPMessages.AddMessage() as the first action in Exceute(), nothing is yielded.

Any suggestions is appreciated..

6
  • Can you create a minimal sample project and upload it somewhere? See this meta question for hosting suggestions.
    – blah238
    Commented Feb 16, 2013 at 15:43
  • Are you releasing COM object references? Might have something to do with it. I seem to remember this being particular important with enumerators.
    – blah238
    Commented Feb 16, 2013 at 15:44
  • Sure, I'll look into that when I get back to my office on monday.
    – Oyvind
    Commented Feb 16, 2013 at 15:45
  • @blah238, good point! I'll try that when I get back to it.
    – Oyvind
    Commented Feb 16, 2013 at 15:46
  • I'm using IGpMessages.Add() without any problems. I did have a problem but resolved it by instantiating a new IGpMessage instead of re-using. Commented Feb 16, 2013 at 18:50

1 Answer 1

1

Resolved this when attaching the debugger to the background process.

For reasons I don't know, IGPUtilities.GetMapLayers() returns null when used inside UpdateMessages() right before Execute() is being triggered as a background job. This caused a NullPointerException to be thrown.

Still, ArcGis happily pretends that the tool succeeded in 2 seconds.

I work this around now by checking the return value from IGPUtilities.GetMapLayers().

2
  • I guess that makes sense. When you run a GP tool in the background it does not have access to the map AFAIK.
    – blah238
    Commented Feb 18, 2013 at 16:58
  • Sure, that would make sense if that code ran inside Execute(). I did the code in UpdateMessages(), which should not run in the background. The fact that I get "Tool succeeded" here is somewhat confusing.
    – Oyvind
    Commented Feb 18, 2013 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.