Take the 2-minute tour ×

To make things simple, my Delphi XE application contains objects in charge of generating files based on some conditions: one object can generate a text file, other objects (which inherits from the former) can generate an XML or HTML files and so on... All of them have a common ancestor but do not run on a thread!

I'm now trying to add a way to cancel the whole generation process whenever the user hits the cancel button. The problem is that I've got nothing in place for this to work. The most basic action would be to add A LOT of if user_canceled then halt_process all over the place throughout the generation process but I feel this isn't the optimal solution.

My question, is to know if there is some smarter way of doing this ? Or any recommendation you might have on refactoring my code so that this addition could be better integrated in the future when more generation code (and therefore cancellation tests) needs to be added ?

share|improve this question
You need to add cancellation checks. –  David Heffernan Jun 26 '12 at 12:02
Adding if Cancelled then exit; explicitly is better style than using exceptions. It leads to a more predictable runtime behaviour. –  Warren P Jun 26 '12 at 14:16
@Warren Not so. That only allows you to cancel from the top level routine. Using the silent EAbort exception is the way to cancel things. –  David Heffernan Jun 26 '12 at 14:42

2 Answers 2

up vote 2 down vote accepted

You will need to add manual checks if process needs to be canceled because there's no way to cancel it and leave the program in valid state. Compiler/Program just does not knows which operations are atomic and which not in your applications logic.


OpenFile(...);  //<- if program halts anywhere before CloseFile you have the file locked

Optimal solution depends on your existing design a lot. In simple case of queued actions A -> B -> C -> D it would work if you wrap it into a thread and add if Terminated then Exit; between actions (of course properly freeing temp objects if any).

share|improve this answer
Thanks @Krom Stern, this makes sense. –  jonjbar Jun 28 '12 at 7:47

I believe that a nice way of implementing a cancel is to use EAbort. As long as you wrap your file open and close in try finally, raising EAbort (eg on a button click)will get you out of a long list of actions quite nicely. It shoud'nt work but it does.

share|improve this answer

Your Answer


