I have a BackgroundWorker that calls a method that calls a method… and somewhere along the line I check for CancellationPending and want to exit the BackgroundWorker.
Is there a way to do that directly from that deeply nested method or does it have to return to its caller, which in turn will return to its caller…?
There is nothing special about the fact that you’re using a
BackgroundWorker– think about this in the same terms as you would just dealing with deeply nested method calls.In other words, no, there’s no (non-horrible) way I can think of to do that directly.
The cleanest way I can think of is to simply check for
CancellationPendingimmediately after you return from each nested method, at every level (and iftrue, return).Throwing a specific exception and catching same specific exception type at the top level would get you out quickly but isn’t exactly best practice (which is, don’t use exceptions anything non-exceptional, like normal flow control).