Since this topic was just brought up again, and it is a relatively popular and important topic, I decided it may be a good time to drop an additional 'Tip' or few in here about it, so that others to find this topic again will know about them.
If your process is a relatively long one (many iterations), &/or is a relatively complex one that may require a lot of processing, &/or may have opportunities for errors or unforeseen complications, then you may want some additional control over the situation. This is because sometimes processes can take much longer than expected, or may cause Inventor to seemingly 'freeze' (become unresponsive), or you remembered a detail you forgot in the code process just after starting the process, or simply want to stop the process before it finishes for whatever reason. In cases like that, I recommend that when using the CreateProgressBar method, you specify True for the optional fourth input parameter named "AllowCancel". Doing so should make the Cancel button visible in the ProgressBar dialog. However, at this point, clicking that cancel button will only cancel the ProgressBar object itself, because that ProgressBar object does not know what 'process' you are doing that needs to be stopped, it only knows how many steps there are supposed to be. So, to make this cancel button's functionality more useful / beneficual, we will need to incorporate at least two 'outside' resources into our overall code that are defined outside of the 'main' routine. One of those resources would be a separate Sub routine, designed to be the 'event handler' (routine designated to run in response to an Event happening) for the ProgressBar.OnCancel Event. Another resource will be a Boolean type variable, which will indicate the current status of whether we want the process to be canceled or not. One of the things the code within the event handler Sub routine will do is set the value of that Boolean variable. Now, with those two recourses in place, we have an extra thing to 'check' within the 'iteration' of 'the process'. Just inside the iteration loop of the main process code, before we even deal with the ProgressBar.Message or the ProgressBar.UpdateProgress method, we would check the value of that special Boolean type variable. And if it indicates that the cancel button has been used, or the ProgressBar has been canceled, then use a line of code like "Exit For" or "Exit Do", or "Exit Sub", to either exit that iteration, or exit the entire code process.
@JelteDeJong has a good Blog post about this strategy with a code example. It also includes the use of a third outside tool which you may like.
The link to that post is below:
http://www.hjalte.nl/64-stop-long-running-rule
Wesley Crihfield

(Not an Autodesk Employee)