简体   繁体   中英

What options do I have to cancel a long list of actions in a Delphi appliction?

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.

Eg

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

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM