chkaufmann 17 Posted July 31, 2019 In my application I have some long running tasks and I'm not sure what is the better way to abort these. I have a global context (per thread) where I set an "aborted" flag when the user pressed "Cancel". 1) Should I check this flag in each loop (while, for, repeat)? 2) Should I check the flag in the most called loops only and then throw an abort exception? I tend to do 1) because then I have better control over the code flow, but what do you think? Christian Share this post Link to post
Guest Posted July 31, 2019 (edited) Only the task itself - so it is you the programmer - can decide when it is safe to abort the operation and if there is some cleanup to do on cancellation. So, whenever you are at a point inside the task code where you can safely abort the execution of the task, check for cancellation. Edited July 31, 2019 by Guest Share this post Link to post
chkaufmann 17 Posted August 1, 2019 Yes that's clear, but for me the question remains if I should check the flag in each loop down through all code or if I should work with abort exception. Christian Share this post Link to post
Stefan Glienke 2006 Posted August 1, 2019 Passing a token to the task that can be checked and raise a special "task cancelled" exception when that is the case which gets cought in the task executing layer. Of course write your code executed in a task so that it can be cancelled via exception Share this post Link to post
Guest Posted August 1, 2019 (edited) 30 minutes ago, chkaufmann said: Yes that's clear, but for me the question remains if I should check the flag in each loop down through all code or if I should work with abort exception. Check it at any place where it is safe to cancel. You can do that with ITask.CheckCanceled which will throw the EOperationCanceled exception if the task is requested to cancel. If it is safe to call it in each loop -> call it in each loop. Edited August 1, 2019 by Guest Share this post Link to post