Select to view content in your preferred language

Cancel event in Python Gp tools with arcpy

429
2
04-25-2013 01:51 AM
DanielIsenegger
Emerging Contributor
In GP-tools written in C# (ArcObjects) we can listen for the Cancel-event and react on that. I wonder if there is anything similar in Python GP-tools applying arcpy. I googled that issue and the only thing I found was the Cancel-method on the Result-Object (so that you can ask the state of the result of a GP-tool, i.e. if it was canceled). I'm aware that you can cancel GP-tools running in the background in the Result Window, but this Cancel-event is catched by the system and therefore the developer or the GP-tool cannot react on that.
Further I tried to catch the arcpy.ExecuteError within the canceled Python GP-script with the following statement, but no exception was catched:
        try:
            for workspace in workspace_list:
                   
               counter += 1
                   
                msg = str.format("{0}.workspace: {1} ", counter, workspace)
                whole_msg += msg
                   
                arcpy.AddMessage(msg)

        except arcpy.ExecuteError:
            msg = str.format("arcpy.ExecuteError: {0}", arcpy.GetMessages())
            arcpy.AddError(msg)
               
        # Return any other type of error
        except:
            msg = str.format("Non-tool error occurred: {0}", arcpy.GetMessages())
            arcpy.AddError(msg)

Thank You in advance!
Tags (2)
2 Replies
ZibusisoNcube
New Contributor
In GP-tools written in C# (ArcObjects) we can listen for the Cancel-event and react on that. I wonder if there is anything similar in Python GP-tools applying arcpy. I googled that issue and the only thing I found was the Cancel-method on the Result-Object (so that you can ask the state of the result of a GP-tool, i.e. if it was canceled). I'm aware that you can cancel GP-tools running in the background in the Result Window, but this Cancel-event is catched by the system and therefore the developer or the GP-tool cannot react on that.
Further I tried to catch the arcpy.ExecuteError within the canceled Python GP-script with the following statement, but no exception was catched:
        try:
            for workspace in workspace_list:
                   
               counter += 1
                   
                msg = str.format("{0}.workspace: {1} ", counter, workspace)
                whole_msg += msg
                   
                arcpy.AddMessage(msg)

        except arcpy.ExecuteError:
            msg = str.format("arcpy.ExecuteError: {0}", arcpy.GetMessages())
            arcpy.AddError(msg)
               
        # Return any other type of error
        except:
            msg = str.format("Non-tool error occurred: {0}", arcpy.GetMessages())
            arcpy.AddError(msg)

Thank You in advance!


Did you ever get a solution to your problem. If ou did kindly assist by posting it here. I am in a similar dilemma

Thank YOU...in advance 🙂
0 Kudos
DanielIsenegger
Emerging Contributor
Hi
I posted this question also on other forums like gis.stackexchange.com, but no, unfortunately till now, no replies and no solutions or workarounds. It seems that this problem is not causing much troubles to GP-Tools users.

Regards,
Dani
0 Kudos