AnsweredAssumed Answered

python capture the Cancel button and terminate gracefully

Question asked by griner on Nov 2, 2018
Latest reply on Nov 3, 2018 by bixb0012

Seems a simple question: My python tool is looping through records and outputting a "<current record> being processed" message in the tool's running dialog box. As the tool loops through the <records> it's generating some output file (in this case an excel file). I'm using the SetProgressor/SetProgressorPosition feature of arcpy to show progress through the loop.  But now my users understandably want to be able to click on the 'Cancel' button in the upper right corner of the dialog box and have the tool stop running, but still save the results of the <records> that were processed.

This site (arcpy - Forcing Python Toolbox tool to break loop and do cleanup when user clicks Cancel? - Geographic Information Syste… ) claims that you can wrap the arcpy.SetProgressorPosition() call in a try/except construct and that the except clause gets executed if the user has clicked on the Cancel button.  (I'm running ArcGIS Desktop 10.4.1 in Windows 10)

I do not finding the above technique to work.

How does an arcpy script capture the user clicking on 'Cancel' and having a chance to clean-up before exiting?