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 ?
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.
E.g.
Optimal solution depends on your existing design a lot. In simple case of queued actions
A -> B -> C -> Dit would work if you wrap it into a thread and addif Terminated then Exit;between actions (of course properly freeing temp objects if any).