I have a large segment of code that is not a loop, just a list of commands that happens once but takes some time. I need it to either pause or terminate this at any point based on a changing boolean value. I could use a different thread to suspend, resume and stop this code, but those methods are deprecated, so I would like to avoid using them. I could check the boolean between every line of code, but I am hoping for a more elegant solution. Is there a good way to do this?