I assume your job is using a loop, and you want to break it once a user requested job abort. And I assume that the method has a parameter for identifier of interruptible work. In this case you can add a column, say
Canceled to your entity and check its value on each iteration. This will add some delays, but it is required to use shared storage here to pass the job cancellation.
The example below uses some kind of EntityFramework, but I forgot all its method names
public void SendMail(int mailListId)
var mailList = _context.MailLists.Find(mailListId);
foreach (var email in mailList.Emails)
// We need to always get the freshest value, not the cached one.
To cancel a job, you should set the
Canceled column value to
true. If you want to see the job in the
Deleted state, then just delete it. Otherwise it will be moved to the
Succeeded state, that can be confusing a bit.
public void Cancel()
var mailList = _context.MailLists.Find(id);
mailList.Canceled = true;