Jump to content

Recommended Posts

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

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 by Guest

Share this post


Link to post

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

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
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 by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×