How to cancel a backgroundworker with multiple methods in c# optimal?

I have a backgroundworker with a few different functions and loops.
Every example I found on the internet is only with one loop respectively one function.
Now I want to know if there exists a short solution to cancel the worker with a button.
I only know two solutions which are not optimal I think.

When working with a background worker, really the only choice you have is to pepper your code with checks to see if the operation has been cancelled.

How you implement this is entirely up to you, but I'd say you want to check at the start of every potentially-time-consuming operation, e.g. a database query or file system operation, and definitely at the beginning of every loop.

Now, as you've already noticed, it gets trickier when you have multiple methods involved. In that case, storing your worker in a class-level field is probably appropriate so that you can simply check its cancellation status from every method wherever applicable.

One useful approach would be to utilize your own custom Exception class. You could wrap the code in the top-level method within a try-catch block, and then whenever you find out that the background worker has been cancelled somewhere deeper within the call stack, you could just throw an instance of your exception which would be handled by the catch block.